]> git.sesse.net Git - ffmpeg/blob - libavcodec/libschroedingerdec.c
atrac3: switch to av_assert
[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
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 "libavutil/imgutils.h"
31 #include "libavutil/intreadwrite.h"
32 #include "avcodec.h"
33 #include "libschroedinger.h"
34
35 #include <schroedinger/schro.h>
36 #include <schroedinger/schrodebug.h>
37 #include <schroedinger/schrovideoformat.h>
38
39 /** SchroFrame and Pts relation */
40 typedef struct LibSchroFrameContext {
41      SchroFrame *frame;
42      int64_t pts;
43 } LibSchroFrameContext;
44
45 /** libschroedinger decoder private data */
46 typedef struct SchroDecoderParams {
47     /** Schroedinger video format */
48     SchroVideoFormat *format;
49
50     /** Schroedinger frame format */
51     SchroFrameFormat frame_format;
52
53     /** decoder handle */
54     SchroDecoder* decoder;
55
56     /** queue storing decoded frames */
57     FFSchroQueue dec_frame_queue;
58
59     /** end of sequence signalled */
60     int eos_signalled;
61
62     /** end of sequence pulled */
63     int eos_pulled;
64
65     /** decoded picture */
66     AVFrame dec_frame;
67 } SchroDecoderParams;
68
69 typedef struct SchroParseUnitContext {
70     const uint8_t *buf;
71     int           buf_size;
72 } SchroParseUnitContext;
73
74
75 static void libschroedinger_decode_buffer_free(SchroBuffer *schro_buf,
76                                                void *priv)
77 {
78     av_freep(&priv);
79 }
80
81 static void parse_context_init(SchroParseUnitContext *parse_ctx,
82                                const uint8_t *buf, int buf_size)
83 {
84     parse_ctx->buf           = buf;
85     parse_ctx->buf_size      = buf_size;
86 }
87
88 static SchroBuffer *find_next_parse_unit(SchroParseUnitContext *parse_ctx)
89 {
90     SchroBuffer *enc_buf = NULL;
91     int next_pu_offset = 0;
92     unsigned char *in_buf;
93
94     if (parse_ctx->buf_size < 13 ||
95         parse_ctx->buf[0] != 'B' ||
96         parse_ctx->buf[1] != 'B' ||
97         parse_ctx->buf[2] != 'C' ||
98         parse_ctx->buf[3] != 'D')
99         return NULL;
100
101     next_pu_offset = (parse_ctx->buf[5] << 24) +
102                      (parse_ctx->buf[6] << 16) +
103                      (parse_ctx->buf[7] <<  8) +
104                       parse_ctx->buf[8];
105
106     if (next_pu_offset == 0 &&
107         SCHRO_PARSE_CODE_IS_END_OF_SEQUENCE(parse_ctx->buf[4]))
108         next_pu_offset = 13;
109
110     if (next_pu_offset <= 0 || parse_ctx->buf_size < next_pu_offset)
111         return NULL;
112
113     in_buf = av_malloc(next_pu_offset);
114     if (!in_buf) {
115         av_log(parse_ctx, AV_LOG_ERROR, "Unable to allocate input buffer\n");
116         return NULL;
117     }
118
119     memcpy(in_buf, parse_ctx->buf, next_pu_offset);
120     enc_buf       = schro_buffer_new_with_data(in_buf, next_pu_offset);
121     enc_buf->free = libschroedinger_decode_buffer_free;
122     enc_buf->priv = in_buf;
123
124     parse_ctx->buf      += next_pu_offset;
125     parse_ctx->buf_size -= next_pu_offset;
126
127     return enc_buf;
128 }
129
130 /**
131 * Returns FFmpeg chroma format.
132 */
133 static enum PixelFormat get_chroma_format(SchroChromaFormat schro_pix_fmt)
134 {
135     int num_formats = sizeof(schro_pixel_format_map) /
136                       sizeof(schro_pixel_format_map[0]);
137     int idx;
138
139     for (idx = 0; idx < num_formats; ++idx)
140         if (schro_pixel_format_map[idx].schro_pix_fmt == schro_pix_fmt)
141             return schro_pixel_format_map[idx].ff_pix_fmt;
142     return PIX_FMT_NONE;
143 }
144
145 static av_cold int libschroedinger_decode_init(AVCodecContext *avccontext)
146 {
147
148     SchroDecoderParams *p_schro_params = avccontext->priv_data;
149     /* First of all, initialize our supporting libraries. */
150     schro_init();
151
152     schro_debug_set_level(avccontext->debug);
153     p_schro_params->decoder = schro_decoder_new();
154     schro_decoder_set_skip_ratio(p_schro_params->decoder, 1);
155
156     if (!p_schro_params->decoder)
157         return -1;
158
159     /* Initialize the decoded frame queue. */
160     ff_schro_queue_init(&p_schro_params->dec_frame_queue);
161     return 0;
162 }
163
164 static void libschroedinger_decode_frame_free(void *frame)
165 {
166     schro_frame_unref(frame);
167 }
168
169 static void libschroedinger_handle_first_access_unit(AVCodecContext *avccontext)
170 {
171     SchroDecoderParams *p_schro_params = avccontext->priv_data;
172     SchroDecoder *decoder = p_schro_params->decoder;
173
174     p_schro_params->format = schro_decoder_get_video_format(decoder);
175
176     /* Tell FFmpeg about sequence details. */
177     if (av_image_check_size(p_schro_params->format->width,
178                             p_schro_params->format->height, 0, avccontext) < 0) {
179         av_log(avccontext, AV_LOG_ERROR, "invalid dimensions (%dx%d)\n",
180                p_schro_params->format->width, p_schro_params->format->height);
181         avccontext->height = avccontext->width = 0;
182         return;
183     }
184     avccontext->height  = p_schro_params->format->height;
185     avccontext->width   = p_schro_params->format->width;
186     avccontext->pix_fmt = get_chroma_format(p_schro_params->format->chroma_format);
187
188     if (ff_get_schro_frame_format(p_schro_params->format->chroma_format,
189                                   &p_schro_params->frame_format) == -1) {
190         av_log(avccontext, AV_LOG_ERROR,
191                "This codec currently only supports planar YUV 4:2:0, 4:2:2 "
192                "and 4:4:4 formats.\n");
193         return;
194     }
195
196     avccontext->time_base.den = p_schro_params->format->frame_rate_numerator;
197     avccontext->time_base.num = p_schro_params->format->frame_rate_denominator;
198 }
199
200 static int libschroedinger_decode_frame(AVCodecContext *avccontext,
201                                         void *data, int *data_size,
202                                         AVPacket *avpkt)
203 {
204     const uint8_t *buf = avpkt->data;
205     int buf_size = avpkt->size;
206     int64_t pts  = avpkt->pts;
207     SchroTag *tag;
208
209     SchroDecoderParams *p_schro_params = avccontext->priv_data;
210     SchroDecoder *decoder = p_schro_params->decoder;
211     SchroBuffer *enc_buf;
212     SchroFrame* frame;
213     int state;
214     int go = 1;
215     int outer = 1;
216     SchroParseUnitContext parse_ctx;
217     LibSchroFrameContext *framewithpts = NULL;
218
219     *data_size = 0;
220
221     parse_context_init(&parse_ctx, buf, buf_size);
222     if (!buf_size) {
223         if (!p_schro_params->eos_signalled) {
224             state = schro_decoder_push_end_of_stream(decoder);
225             p_schro_params->eos_signalled = 1;
226         }
227     }
228
229     /* Loop through all the individual parse units in the input buffer */
230     do {
231         if ((enc_buf = find_next_parse_unit(&parse_ctx))) {
232             /* Set Schrotag with the pts to be recovered after decoding*/
233             enc_buf->tag = schro_tag_new(av_malloc(sizeof(int64_t)), av_free);
234             if (!enc_buf->tag->value) {
235                 av_log(avccontext, AV_LOG_ERROR, "Unable to allocate SchroTag\n");
236                 return AVERROR(ENOMEM);
237             }
238             AV_WN(64, enc_buf->tag->value, pts);
239             /* Push buffer into decoder. */
240             if (SCHRO_PARSE_CODE_IS_PICTURE(enc_buf->data[4]) &&
241                 SCHRO_PARSE_CODE_NUM_REFS(enc_buf->data[4]) > 0)
242                 avccontext->has_b_frames = 1;
243             state = schro_decoder_push(decoder, enc_buf);
244             if (state == SCHRO_DECODER_FIRST_ACCESS_UNIT)
245                 libschroedinger_handle_first_access_unit(avccontext);
246             go = 1;
247         } else
248             outer = 0;
249
250         while (go) {
251             /* Parse data and process result. */
252             state = schro_decoder_wait(decoder);
253             switch (state) {
254             case SCHRO_DECODER_FIRST_ACCESS_UNIT:
255                 libschroedinger_handle_first_access_unit(avccontext);
256                 break;
257
258             case SCHRO_DECODER_NEED_BITS:
259                 /* Need more input data - stop iterating over what we have. */
260                 go = 0;
261                 break;
262
263             case SCHRO_DECODER_NEED_FRAME:
264                 /* Decoder needs a frame - create one and push it in. */
265                 frame = ff_create_schro_frame(avccontext,
266                                               p_schro_params->frame_format);
267                 schro_decoder_add_output_picture(decoder, frame);
268                 break;
269
270             case SCHRO_DECODER_OK:
271                 /* Pull a frame out of the decoder. */
272                 tag   = schro_decoder_get_picture_tag(decoder);
273                 frame = schro_decoder_pull(decoder);
274
275                 if (frame) {
276                     /* Add relation between schroframe and pts. */
277                     framewithpts = av_malloc(sizeof(LibSchroFrameContext));
278                     if (!framewithpts) {
279                         av_log(avccontext, AV_LOG_ERROR, "Unable to allocate FrameWithPts\n");
280                         return AVERROR(ENOMEM);
281                     }
282                     framewithpts->frame = frame;
283                     framewithpts->pts   = AV_RN64(tag->value);
284                     ff_schro_queue_push_back(&p_schro_params->dec_frame_queue,
285                                              framewithpts);
286                 }
287                 break;
288             case SCHRO_DECODER_EOS:
289                 go = 0;
290                 p_schro_params->eos_pulled = 1;
291                 schro_decoder_reset(decoder);
292                 outer = 0;
293                 break;
294
295             case SCHRO_DECODER_ERROR:
296                 return -1;
297                 break;
298             }
299         }
300     } while (outer);
301
302     /* Grab next frame to be returned from the top of the queue. */
303     framewithpts = ff_schro_queue_pop(&p_schro_params->dec_frame_queue);
304
305     if (framewithpts && framewithpts->frame) {
306         if (p_schro_params->dec_frame.data[0])
307             avccontext->release_buffer(avccontext, &p_schro_params->dec_frame);
308         if (avccontext->get_buffer(avccontext, &p_schro_params->dec_frame) < 0) {
309             av_log(avccontext, AV_LOG_ERROR, "Unable to allocate buffer\n");
310             return AVERROR(ENOMEM);
311         }
312
313         memcpy(p_schro_params->dec_frame.data[0],
314                framewithpts->frame->components[0].data,
315                framewithpts->frame->components[0].length);
316
317         memcpy(p_schro_params->dec_frame.data[1],
318                framewithpts->frame->components[1].data,
319                framewithpts->frame->components[1].length);
320
321         memcpy(p_schro_params->dec_frame.data[2],
322                framewithpts->frame->components[2].data,
323                framewithpts->frame->components[2].length);
324
325         /* Fill frame with current buffer data from Schroedinger. */
326         p_schro_params->dec_frame.format  = -1; /* Unknown -1 */
327         p_schro_params->dec_frame.width   = framewithpts->frame->width;
328         p_schro_params->dec_frame.height  = framewithpts->frame->height;
329         p_schro_params->dec_frame.pkt_pts = framewithpts->pts;
330         p_schro_params->dec_frame.linesize[0] = framewithpts->frame->components[0].stride;
331         p_schro_params->dec_frame.linesize[1] = framewithpts->frame->components[1].stride;
332         p_schro_params->dec_frame.linesize[2] = framewithpts->frame->components[2].stride;
333
334         *(AVFrame*)data = p_schro_params->dec_frame;
335         *data_size      = sizeof(AVFrame);
336
337         /* Now free the frame resources. */
338         libschroedinger_decode_frame_free(framewithpts->frame);
339         av_free(framewithpts);
340     } else {
341         data       = NULL;
342         *data_size = 0;
343     }
344     return buf_size;
345 }
346
347
348 static av_cold int libschroedinger_decode_close(AVCodecContext *avccontext)
349 {
350     SchroDecoderParams *p_schro_params = avccontext->priv_data;
351     /* Free the decoder. */
352     schro_decoder_free(p_schro_params->decoder);
353     av_freep(&p_schro_params->format);
354
355     if (p_schro_params->dec_frame.data[0])
356         avccontext->release_buffer(avccontext, &p_schro_params->dec_frame);
357
358     /* Free data in the output frame queue. */
359     ff_schro_queue_free(&p_schro_params->dec_frame_queue,
360                         libschroedinger_decode_frame_free);
361
362     return 0;
363 }
364
365 static void libschroedinger_flush(AVCodecContext *avccontext)
366 {
367     /* Got a seek request. Free the decoded frames queue and then reset
368      * the decoder */
369     SchroDecoderParams *p_schro_params = avccontext->priv_data;
370
371     /* Free data in the output frame queue. */
372     ff_schro_queue_free(&p_schro_params->dec_frame_queue,
373                         libschroedinger_decode_frame_free);
374
375     ff_schro_queue_init(&p_schro_params->dec_frame_queue);
376     schro_decoder_reset(p_schro_params->decoder);
377     p_schro_params->eos_pulled = 0;
378     p_schro_params->eos_signalled = 0;
379 }
380
381 AVCodec ff_libschroedinger_decoder = {
382     .name           = "libschroedinger",
383     .type           = AVMEDIA_TYPE_VIDEO,
384     .id             = CODEC_ID_DIRAC,
385     .priv_data_size = sizeof(SchroDecoderParams),
386     .init           = libschroedinger_decode_init,
387     .close          = libschroedinger_decode_close,
388     .decode         = libschroedinger_decode_frame,
389     .capabilities   = CODEC_CAP_DELAY,
390     .flush          = libschroedinger_flush,
391     .long_name      = NULL_IF_CONFIG_SMALL("libschroedinger Dirac 2.2"),
392 };