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 Libav.
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.
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.
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
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).
32 #include "libavutil/imgutils.h"
33 #include "libavutil/internal.h"
34 #include "libavutil/intreadwrite.h"
35 #include "libavutil/mem.h"
38 #include "libschroedinger.h"
44 #include <schroedinger/schro.h>
45 #include <schroedinger/schrodebug.h>
46 #include <schroedinger/schrovideoformat.h>
48 /** SchroFrame and Pts relation */
49 typedef struct LibSchroFrameContext {
52 } LibSchroFrameContext;
54 /** libschroedinger decoder private data */
55 typedef struct SchroDecoderParams {
56 /** Schroedinger video format */
57 SchroVideoFormat *format;
59 /** Schroedinger frame format */
60 SchroFrameFormat frame_format;
63 SchroDecoder* decoder;
65 /** queue storing decoded frames */
66 FFSchroQueue dec_frame_queue;
68 /** end of sequence signalled */
71 /** end of sequence pulled */
75 typedef struct SchroParseUnitContext {
78 } SchroParseUnitContext;
81 static void libschroedinger_decode_buffer_free(SchroBuffer *schro_buf,
87 static void parse_context_init(SchroParseUnitContext *parse_ctx,
88 const uint8_t *buf, int buf_size)
91 parse_ctx->buf_size = buf_size;
94 static SchroBuffer *find_next_parse_unit(SchroParseUnitContext *parse_ctx)
96 SchroBuffer *enc_buf = NULL;
97 int next_pu_offset = 0;
98 unsigned char *in_buf;
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')
107 next_pu_offset = (parse_ctx->buf[5] << 24) +
108 (parse_ctx->buf[6] << 16) +
109 (parse_ctx->buf[7] << 8) +
112 if (next_pu_offset == 0 &&
113 SCHRO_PARSE_CODE_IS_END_OF_SEQUENCE(parse_ctx->buf[4]))
116 if (next_pu_offset <= 0 || parse_ctx->buf_size < next_pu_offset)
119 in_buf = av_malloc(next_pu_offset);
121 av_log(parse_ctx, AV_LOG_ERROR, "Unable to allocate input buffer\n");
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;
130 parse_ctx->buf += next_pu_offset;
131 parse_ctx->buf_size -= next_pu_offset;
137 * Returns Libav chroma format.
139 static enum AVPixelFormat get_chroma_format(SchroChromaFormat schro_pix_fmt)
141 int num_formats = sizeof(schro_pixel_format_map) /
142 sizeof(schro_pixel_format_map[0]);
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;
151 static av_cold int libschroedinger_decode_init(AVCodecContext *avctx)
154 SchroDecoderParams *p_schro_params = avctx->priv_data;
155 /* First of all, initialize our supporting libraries. */
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);
162 if (!p_schro_params->decoder)
165 /* Initialize the decoded frame queue. */
166 ff_schro_queue_init(&p_schro_params->dec_frame_queue);
170 static void libschroedinger_decode_frame_free(void *frame)
172 schro_frame_unref(frame);
175 static void libschroedinger_handle_first_access_unit(AVCodecContext *avctx)
177 SchroDecoderParams *p_schro_params = avctx->priv_data;
178 SchroDecoder *decoder = p_schro_params->decoder;
180 p_schro_params->format = schro_decoder_get_video_format(decoder);
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;
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);
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");
202 avctx->time_base.den = p_schro_params->format->frame_rate_numerator;
203 avctx->time_base.num = p_schro_params->format->frame_rate_denominator;
206 static int libschroedinger_decode_frame(AVCodecContext *avctx,
207 void *data, int *got_frame,
210 const uint8_t *buf = avpkt->data;
211 int buf_size = avpkt->size;
212 int64_t pts = avpkt->pts;
215 SchroDecoderParams *p_schro_params = avctx->priv_data;
216 SchroDecoder *decoder = p_schro_params->decoder;
217 SchroBuffer *enc_buf;
219 AVFrame *avframe = data;
223 SchroParseUnitContext parse_ctx;
224 LibSchroFrameContext *framewithpts = NULL;
228 parse_context_init(&parse_ctx, buf, 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;
236 /* Loop through all the individual parse units in the input buffer */
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);
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);
258 /* Parse data and process result. */
259 state = schro_decoder_wait(decoder);
261 case SCHRO_DECODER_FIRST_ACCESS_UNIT:
262 libschroedinger_handle_first_access_unit(avctx);
265 case SCHRO_DECODER_NEED_BITS:
266 /* Need more input data - stop iterating over what we have. */
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 schro_decoder_add_output_picture(decoder, frame);
277 case SCHRO_DECODER_OK:
278 /* Pull a frame out of the decoder. */
279 tag = schro_decoder_get_picture_tag(decoder);
280 frame = schro_decoder_pull(decoder);
283 /* Add relation between schroframe and pts. */
284 framewithpts = av_malloc(sizeof(LibSchroFrameContext));
286 av_log(avctx, AV_LOG_ERROR, "Unable to allocate FrameWithPts\n");
287 return AVERROR(ENOMEM);
289 framewithpts->frame = frame;
290 framewithpts->pts = AV_RN64(tag->value);
291 ff_schro_queue_push_back(&p_schro_params->dec_frame_queue,
295 case SCHRO_DECODER_EOS:
297 p_schro_params->eos_pulled = 1;
298 schro_decoder_reset(decoder);
302 case SCHRO_DECODER_ERROR:
309 /* Grab next frame to be returned from the top of the queue. */
310 framewithpts = ff_schro_queue_pop(&p_schro_params->dec_frame_queue);
312 if (framewithpts && framewithpts->frame) {
313 if (ff_get_buffer(avctx, avframe, 0) < 0) {
314 av_log(avctx, AV_LOG_ERROR, "Unable to allocate buffer\n");
315 return AVERROR(ENOMEM);
318 memcpy(avframe->data[0],
319 framewithpts->frame->components[0].data,
320 framewithpts->frame->components[0].length);
322 memcpy(avframe->data[1],
323 framewithpts->frame->components[1].data,
324 framewithpts->frame->components[1].length);
326 memcpy(avframe->data[2],
327 framewithpts->frame->components[2].data,
328 framewithpts->frame->components[2].length);
330 /* Fill frame with current buffer data from Schroedinger. */
331 avframe->pkt_pts = framewithpts->pts;
332 avframe->linesize[0] = framewithpts->frame->components[0].stride;
333 avframe->linesize[1] = framewithpts->frame->components[1].stride;
334 avframe->linesize[2] = framewithpts->frame->components[2].stride;
338 /* Now free the frame resources. */
339 libschroedinger_decode_frame_free(framewithpts->frame);
340 av_free(framewithpts);
349 static av_cold int libschroedinger_decode_close(AVCodecContext *avctx)
351 SchroDecoderParams *p_schro_params = avctx->priv_data;
352 /* Free the decoder. */
353 schro_decoder_free(p_schro_params->decoder);
354 av_freep(&p_schro_params->format);
356 /* Free data in the output frame queue. */
357 ff_schro_queue_free(&p_schro_params->dec_frame_queue,
358 libschroedinger_decode_frame_free);
363 static void libschroedinger_flush(AVCodecContext *avctx)
365 /* Got a seek request. Free the decoded frames queue and then reset
367 SchroDecoderParams *p_schro_params = avctx->priv_data;
369 /* Free data in the output frame queue. */
370 ff_schro_queue_free(&p_schro_params->dec_frame_queue,
371 libschroedinger_decode_frame_free);
373 ff_schro_queue_init(&p_schro_params->dec_frame_queue);
374 schro_decoder_reset(p_schro_params->decoder);
375 p_schro_params->eos_pulled = 0;
376 p_schro_params->eos_signalled = 0;
379 AVCodec ff_libschroedinger_decoder = {
380 .name = "libschroedinger",
381 .type = AVMEDIA_TYPE_VIDEO,
382 .id = AV_CODEC_ID_DIRAC,
383 .priv_data_size = sizeof(SchroDecoderParams),
384 .init = libschroedinger_decode_init,
385 .close = libschroedinger_decode_close,
386 .decode = libschroedinger_decode_frame,
387 .capabilities = CODEC_CAP_DELAY,
388 .flush = libschroedinger_flush,
389 .long_name = NULL_IF_CONFIG_SMALL("libschroedinger Dirac 2.2"),