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