]> git.sesse.net Git - ffmpeg/blob - libavcodec/libschroedingerdec.c
b03df79c4b99a20e077f4486176eb4ada38f4b77
[ffmpeg] / libavcodec / libschroedingerdec.c
1 /*
2  * Dirac decoder support via Schroedinger libraries
3  * Copyright (c) 2008 BBC, Anuradha Suraparaju <asuraparaju at gmail dot com >
4  *
5  * This file is part of FFmpeg.
6  *
7  * FFmpeg is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU Lesser General Public
9  * License as published by the Free Software Foundation; either
10  * version 2.1 of the License, or (at your option) any later version.
11  *
12  * FFmpeg is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15  * Lesser General Public License for more details.
16  *
17  * You should have received a copy of the GNU Lesser General Public
18  * License along with FFmpeg; if not, write to the Free Software
19  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20  */
21
22 /**
23 * @file libschroedingerdec.c
24 * Dirac decoder support via libschroedinger-1.0 libraries. More details about
25 * the Schroedinger project can be found at http://www.diracvideo.org/.
26 * The library implements Dirac Specification Version 2.2.
27 * (http://dirac.sourceforge.net/specification.html).
28 */
29
30 #include "avcodec.h"
31 #include "libdirac_libschro.h"
32 #include "libschroedinger.h"
33
34 #undef NDEBUG
35 #include <assert.h>
36
37
38 #include <schroedinger/schro.h>
39 #include <schroedinger/schrodebug.h>
40 #include <schroedinger/schrovideoformat.h>
41
42 /** libschroedinger decoder private data */
43 typedef struct FfmpegSchroDecoderParams
44 {
45     /** Schroedinger video format */
46     SchroVideoFormat *format;
47
48     /** Schroedinger frame format */
49     SchroFrameFormat frame_format;
50
51     /** decoder handle */
52     SchroDecoder* decoder;
53
54     /** queue storing decoded frames */
55     FfmpegDiracSchroQueue dec_frame_queue;
56
57     /** end of sequence signalled */
58     int eos_signalled;
59
60     /** end of sequence pulled */
61     int eos_pulled;
62
63     /** decoded picture */
64     AVPicture dec_pic;
65 } FfmpegSchroDecoderParams;
66
67 /**
68 * Returns FFmpeg chroma format.
69 */
70 static enum PixelFormat GetFfmpegChromaFormat(SchroChromaFormat schro_pix_fmt)
71 {
72     int num_formats = sizeof(ffmpeg_schro_pixel_format_map) /
73                       sizeof(ffmpeg_schro_pixel_format_map[0]);
74     int idx;
75
76     for (idx = 0; idx < num_formats; ++idx) {
77         if (ffmpeg_schro_pixel_format_map[idx].schro_pix_fmt == schro_pix_fmt) {
78             return ffmpeg_schro_pixel_format_map[idx].ff_pix_fmt;
79         }
80     }
81     return PIX_FMT_NONE;
82 }
83
84 static int libschroedinger_decode_init(AVCodecContext *avccontext)
85 {
86
87     FfmpegSchroDecoderParams *p_schro_params = avccontext->priv_data ;
88     /* First of all, initialize our supporting libraries. */
89     schro_init();
90
91     schro_debug_set_level(avccontext->debug);
92     p_schro_params->decoder =  schro_decoder_new();
93     schro_decoder_set_skip_ratio(p_schro_params->decoder, 1);
94
95     if (!p_schro_params->decoder)
96         return -1;
97
98     /* Initialize the decoded frame queue. */
99     ff_dirac_schro_queue_init (&p_schro_params->dec_frame_queue);
100     return 0 ;
101 }
102
103 static void libschroedinger_decode_buffer_free (SchroBuffer *schro_buf,
104                                                 void *priv)
105 {
106     av_freep(&priv);
107 }
108
109 static void libschroedinger_decode_frame_free (void *frame)
110 {
111     schro_frame_unref(frame);
112 }
113
114 static void libschroedinger_handle_first_access_unit(AVCodecContext *avccontext)
115 {
116     FfmpegSchroDecoderParams *p_schro_params = avccontext->priv_data;
117     SchroDecoder *decoder = p_schro_params->decoder;
118
119     p_schro_params->format = schro_decoder_get_video_format (decoder);
120
121     /* Tell FFmpeg about sequence details. */
122     if(avcodec_check_dimensions(avccontext, p_schro_params->format->width,
123                                 p_schro_params->format->height) < 0) {
124         av_log(avccontext, AV_LOG_ERROR, "invalid dimensions (%dx%d)\n",
125                p_schro_params->format->width, p_schro_params->format->height);
126         avccontext->height = avccontext->width = 0;
127         return;
128     }
129     avccontext->height  = p_schro_params->format->height;
130     avccontext->width   = p_schro_params->format->width;
131     avccontext->pix_fmt =
132                    GetFfmpegChromaFormat(p_schro_params->format->chroma_format);
133
134     if (ff_get_schro_frame_format( p_schro_params->format->chroma_format,
135                                    &p_schro_params->frame_format) == -1) {
136         av_log (avccontext, AV_LOG_ERROR,
137                 "This codec currently only supports planar YUV 4:2:0, 4:2:2 "
138                 "and 4:4:4 formats.\n");
139         return;
140     }
141
142     avccontext->time_base.den = p_schro_params->format->frame_rate_numerator;
143     avccontext->time_base.num = p_schro_params->format->frame_rate_denominator;
144
145     if (p_schro_params->dec_pic.data[0] == NULL)
146     {
147         avpicture_alloc(&p_schro_params->dec_pic,
148                         avccontext->pix_fmt,
149                         avccontext->width,
150                         avccontext->height);
151     }
152 }
153
154 static int libschroedinger_decode_frame(AVCodecContext *avccontext,
155                                         void *data, int *data_size,
156                                         const uint8_t *buf, int buf_size)
157 {
158
159     FfmpegSchroDecoderParams *p_schro_params = avccontext->priv_data;
160     SchroDecoder *decoder = p_schro_params->decoder;
161     SchroVideoFormat *format;
162     AVPicture *picture = data;
163     SchroBuffer *enc_buf;
164     SchroFrame* frame;
165     int state;
166     int go = 1;
167
168     *data_size = 0;
169
170     if (buf_size>0) {
171         unsigned char *in_buf = av_malloc(buf_size);
172         memcpy (in_buf, buf, buf_size);
173         enc_buf = schro_buffer_new_with_data (in_buf, buf_size);
174         enc_buf->free = libschroedinger_decode_buffer_free;
175         enc_buf->priv = in_buf;
176         /* Push buffer into decoder. */
177         state = schro_decoder_push (decoder, enc_buf);
178         if (state == SCHRO_DECODER_FIRST_ACCESS_UNIT)
179             libschroedinger_handle_first_access_unit(avccontext);
180     } else {
181         if (!p_schro_params->eos_signalled) {
182             state = schro_decoder_push_end_of_stream(decoder);
183             p_schro_params->eos_signalled = 1;
184         }
185     }
186
187     format = p_schro_params->format;
188
189     while (go) {
190         /* Parse data and process result. */
191         state = schro_decoder_wait (decoder);
192         switch (state)
193         {
194         case SCHRO_DECODER_FIRST_ACCESS_UNIT:
195             libschroedinger_handle_first_access_unit (avccontext);
196             break;
197
198         case SCHRO_DECODER_NEED_BITS:
199             /* Need more input data - stop iterating over what we have. */
200             go = 0;
201             break;
202
203         case SCHRO_DECODER_NEED_FRAME:
204             /* Decoder needs a frame - create one and push it in. */
205
206             frame = schro_frame_new_and_alloc(NULL,
207                                               p_schro_params->frame_format,
208                                               format->width,
209                                               format->height);
210             schro_decoder_add_output_picture (decoder, frame);
211             break;
212
213         case SCHRO_DECODER_OK:
214             /* Pull a frame out of the decoder. */
215             frame = schro_decoder_pull (decoder);
216
217             if (frame) {
218                 ff_dirac_schro_queue_push_back(
219                                              &p_schro_params->dec_frame_queue,
220                                              frame);
221             }
222             break;
223         case SCHRO_DECODER_EOS:
224             go = 0;
225             p_schro_params->eos_pulled = 1;
226             schro_decoder_reset (decoder);
227             break;
228
229         case SCHRO_DECODER_ERROR:
230             return -1;
231             break;
232         }
233     }
234
235     /* Grab next frame to be returned from the top of the queue. */
236     frame = ff_dirac_schro_queue_pop(&p_schro_params->dec_frame_queue);
237
238     if (frame != NULL) {
239         memcpy (p_schro_params->dec_pic.data[0],
240                 frame->components[0].data,
241                 frame->components[0].length);
242
243         memcpy (p_schro_params->dec_pic.data[1],
244                 frame->components[1].data,
245                 frame->components[1].length);
246
247         memcpy (p_schro_params->dec_pic.data[2],
248                 frame->components[2].data,
249                 frame->components[2].length);
250
251         /* Fill picture with current buffer data from Schroedinger. */
252         avpicture_fill(picture, p_schro_params->dec_pic.data[0],
253                        avccontext->pix_fmt,
254                        avccontext->width, avccontext->height);
255
256         *data_size = sizeof(AVPicture);
257
258         /* Now free the frame resources. */
259         libschroedinger_decode_frame_free (frame);
260     }
261     return buf_size;
262 }
263
264
265 static int libschroedinger_decode_close(AVCodecContext *avccontext)
266 {
267     FfmpegSchroDecoderParams *p_schro_params = avccontext->priv_data;
268     /* Free the decoder. */
269     schro_decoder_free (p_schro_params->decoder);
270     av_freep(&p_schro_params->format);
271
272     avpicture_free (&p_schro_params->dec_pic);
273
274     /* Free data in the output frame queue. */
275     ff_dirac_schro_queue_free (&p_schro_params->dec_frame_queue,
276                                libschroedinger_decode_frame_free);
277
278     return 0 ;
279 }
280
281 static void libschroedinger_flush (AVCodecContext *avccontext)
282 {
283     /* Got a seek request. Free the decoded frames queue and then reset
284      * the decoder */
285     FfmpegSchroDecoderParams *p_schro_params = avccontext->priv_data;
286
287     /* Free data in the output frame queue. */
288     ff_dirac_schro_queue_free (&p_schro_params->dec_frame_queue,
289                                libschroedinger_decode_frame_free);
290
291     ff_dirac_schro_queue_init (&p_schro_params->dec_frame_queue);
292     schro_decoder_reset(p_schro_params->decoder);
293     p_schro_params->eos_pulled = 0;
294     p_schro_params->eos_signalled = 0;
295 }
296
297 AVCodec libschroedinger_decoder = {
298      "libschroedinger",
299     CODEC_TYPE_VIDEO,
300     CODEC_ID_DIRAC,
301     sizeof(FfmpegSchroDecoderParams),
302     libschroedinger_decode_init,
303     NULL,
304     libschroedinger_decode_close,
305     libschroedinger_decode_frame,
306     CODEC_CAP_DELAY,
307     .flush = libschroedinger_flush
308 };