2 * Dirac decoder support via Schroedinger libraries
3 * Copyright (c) 2008 BBC, Anuradha Suraparaju <asuraparaju at gmail dot com >
5 * This file is part of FFmpeg.
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.
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.
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
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).
30 #include "libavutil/imgutils.h"
31 #include "libavutil/intreadwrite.h"
33 #include "libschroedinger.h"
35 #include <schroedinger/schro.h>
36 #include <schroedinger/schrodebug.h>
37 #include <schroedinger/schrovideoformat.h>
39 /** SchroFrame and Pts relation */
40 typedef struct LibSchroFrameContext {
43 } LibSchroFrameContext;
45 /** libschroedinger decoder private data */
46 typedef struct SchroDecoderParams {
47 /** Schroedinger video format */
48 SchroVideoFormat *format;
50 /** Schroedinger frame format */
51 SchroFrameFormat frame_format;
54 SchroDecoder* decoder;
56 /** queue storing decoded frames */
57 FFSchroQueue dec_frame_queue;
59 /** end of sequence signalled */
62 /** end of sequence pulled */
65 /** decoded picture */
69 typedef struct SchroParseUnitContext {
72 } SchroParseUnitContext;
75 static void libschroedinger_decode_buffer_free(SchroBuffer *schro_buf,
81 static void parse_context_init(SchroParseUnitContext *parse_ctx,
82 const uint8_t *buf, int buf_size)
85 parse_ctx->buf_size = buf_size;
88 static SchroBuffer *find_next_parse_unit(SchroParseUnitContext *parse_ctx)
90 SchroBuffer *enc_buf = NULL;
91 int next_pu_offset = 0;
92 unsigned char *in_buf;
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')
101 next_pu_offset = (parse_ctx->buf[5] << 24) +
102 (parse_ctx->buf[6] << 16) +
103 (parse_ctx->buf[7] << 8) +
106 if (next_pu_offset == 0 &&
107 SCHRO_PARSE_CODE_IS_END_OF_SEQUENCE(parse_ctx->buf[4]))
110 if (next_pu_offset <= 0 || parse_ctx->buf_size < next_pu_offset)
113 in_buf = av_malloc(next_pu_offset);
115 av_log(parse_ctx, AV_LOG_ERROR, "Unable to allocate input buffer\n");
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;
124 parse_ctx->buf += next_pu_offset;
125 parse_ctx->buf_size -= next_pu_offset;
131 * Returns FFmpeg chroma format.
133 static enum PixelFormat get_chroma_format(SchroChromaFormat schro_pix_fmt)
135 int num_formats = sizeof(schro_pixel_format_map) /
136 sizeof(schro_pixel_format_map[0]);
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;
145 static av_cold int libschroedinger_decode_init(AVCodecContext *avccontext)
148 SchroDecoderParams *p_schro_params = avccontext->priv_data;
149 /* First of all, initialize our supporting libraries. */
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);
156 if (!p_schro_params->decoder)
159 /* Initialize the decoded frame queue. */
160 ff_schro_queue_init(&p_schro_params->dec_frame_queue);
164 static void libschroedinger_decode_frame_free(void *frame)
166 schro_frame_unref(frame);
169 static void libschroedinger_handle_first_access_unit(AVCodecContext *avccontext)
171 SchroDecoderParams *p_schro_params = avccontext->priv_data;
172 SchroDecoder *decoder = p_schro_params->decoder;
174 p_schro_params->format = schro_decoder_get_video_format(decoder);
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;
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);
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");
196 avccontext->time_base.den = p_schro_params->format->frame_rate_numerator;
197 avccontext->time_base.num = p_schro_params->format->frame_rate_denominator;
200 static int libschroedinger_decode_frame(AVCodecContext *avccontext,
201 void *data, int *data_size,
204 const uint8_t *buf = avpkt->data;
205 int buf_size = avpkt->size;
206 int64_t pts = avpkt->pts;
209 SchroDecoderParams *p_schro_params = avccontext->priv_data;
210 SchroDecoder *decoder = p_schro_params->decoder;
211 SchroBuffer *enc_buf;
216 SchroParseUnitContext parse_ctx;
217 LibSchroFrameContext *framewithpts = NULL;
221 parse_context_init(&parse_ctx, buf, 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;
229 /* Loop through all the individual parse units in the input buffer */
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);
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);
251 /* Parse data and process result. */
252 state = schro_decoder_wait(decoder);
254 case SCHRO_DECODER_FIRST_ACCESS_UNIT:
255 libschroedinger_handle_first_access_unit(avccontext);
258 case SCHRO_DECODER_NEED_BITS:
259 /* Need more input data - stop iterating over what we have. */
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);
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);
276 /* Add relation between schroframe and pts. */
277 framewithpts = av_malloc(sizeof(LibSchroFrameContext));
279 av_log(avccontext, AV_LOG_ERROR, "Unable to allocate FrameWithPts\n");
280 return AVERROR(ENOMEM);
282 framewithpts->frame = frame;
283 framewithpts->pts = AV_RN64(tag->value);
284 ff_schro_queue_push_back(&p_schro_params->dec_frame_queue,
288 case SCHRO_DECODER_EOS:
290 p_schro_params->eos_pulled = 1;
291 schro_decoder_reset(decoder);
295 case SCHRO_DECODER_ERROR:
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);
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);
313 memcpy(p_schro_params->dec_frame.data[0],
314 framewithpts->frame->components[0].data,
315 framewithpts->frame->components[0].length);
317 memcpy(p_schro_params->dec_frame.data[1],
318 framewithpts->frame->components[1].data,
319 framewithpts->frame->components[1].length);
321 memcpy(p_schro_params->dec_frame.data[2],
322 framewithpts->frame->components[2].data,
323 framewithpts->frame->components[2].length);
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;
334 *(AVFrame*)data = p_schro_params->dec_frame;
335 *data_size = sizeof(AVFrame);
337 /* Now free the frame resources. */
338 libschroedinger_decode_frame_free(framewithpts->frame);
339 av_free(framewithpts);
348 static av_cold int libschroedinger_decode_close(AVCodecContext *avccontext)
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);
355 if (p_schro_params->dec_frame.data[0])
356 avccontext->release_buffer(avccontext, &p_schro_params->dec_frame);
358 /* Free data in the output frame queue. */
359 ff_schro_queue_free(&p_schro_params->dec_frame_queue,
360 libschroedinger_decode_frame_free);
365 static void libschroedinger_flush(AVCodecContext *avccontext)
367 /* Got a seek request. Free the decoded frames queue and then reset
369 SchroDecoderParams *p_schro_params = avccontext->priv_data;
371 /* Free data in the output frame queue. */
372 ff_schro_queue_free(&p_schro_params->dec_frame_queue,
373 libschroedinger_decode_frame_free);
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;
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"),