ae11cc5e2f2a38b6c487d45c071c2160776ceb88
[libav.git] / 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
74 /** decoded picture */
75 AVFrame dec_frame;
76 } SchroDecoderParams;
77
78 typedef struct SchroParseUnitContext {
79 const uint8_t *buf;
80 int buf_size;
81 } SchroParseUnitContext;
82
83
84 static void libschroedinger_decode_buffer_free(SchroBuffer *schro_buf,
85 void *priv)
86 {
87 av_freep(&priv);
88 }
89
90 static void parse_context_init(SchroParseUnitContext *parse_ctx,
91 const uint8_t *buf, int buf_size)
92 {
93 parse_ctx->buf = buf;
94 parse_ctx->buf_size = buf_size;
95 }
96
97 static SchroBuffer *find_next_parse_unit(SchroParseUnitContext *parse_ctx)
98 {
99 SchroBuffer *enc_buf = NULL;
100 int next_pu_offset = 0;
101 unsigned char *in_buf;
102
103 if (parse_ctx->buf_size < 13 ||
104 parse_ctx->buf[0] != 'B' ||
105 parse_ctx->buf[1] != 'B' ||
106 parse_ctx->buf[2] != 'C' ||
107 parse_ctx->buf[3] != 'D')
108 return NULL;
109
110 next_pu_offset = (parse_ctx->buf[5] << 24) +
111 (parse_ctx->buf[6] << 16) +
112 (parse_ctx->buf[7] << 8) +
113 parse_ctx->buf[8];
114
115 if (next_pu_offset == 0 &&
116 SCHRO_PARSE_CODE_IS_END_OF_SEQUENCE(parse_ctx->buf[4]))
117 next_pu_offset = 13;
118
119 if (next_pu_offset <= 0 || parse_ctx->buf_size < next_pu_offset)
120 return NULL;
121
122 in_buf = av_malloc(next_pu_offset);
123 if (!in_buf) {
124 av_log(parse_ctx, AV_LOG_ERROR, "Unable to allocate input buffer\n");
125 return NULL;
126 }
127
128 memcpy(in_buf, parse_ctx->buf, next_pu_offset);
129 enc_buf = schro_buffer_new_with_data(in_buf, next_pu_offset);
130 enc_buf->free = libschroedinger_decode_buffer_free;
131 enc_buf->priv = in_buf;
132
133 parse_ctx->buf += next_pu_offset;
134 parse_ctx->buf_size -= next_pu_offset;
135
136 return enc_buf;
137 }
138
139 /**
140 * Returns Libav chroma format.
141 */
142 static enum AVPixelFormat get_chroma_format(SchroChromaFormat schro_pix_fmt)
143 {
144 int num_formats = sizeof(schro_pixel_format_map) /
145 sizeof(schro_pixel_format_map[0]);
146 int idx;
147
148 for (idx = 0; idx < num_formats; ++idx)
149 if (schro_pixel_format_map[idx].schro_pix_fmt == schro_pix_fmt)
150 return schro_pixel_format_map[idx].ff_pix_fmt;
151 return AV_PIX_FMT_NONE;
152 }
153
154 static av_cold int libschroedinger_decode_init(AVCodecContext *avccontext)
155 {
156
157 SchroDecoderParams *p_schro_params = avccontext->priv_data;
158 /* First of all, initialize our supporting libraries. */
159 schro_init();
160
161 schro_debug_set_level(avccontext->debug);
162 p_schro_params->decoder = schro_decoder_new();
163 schro_decoder_set_skip_ratio(p_schro_params->decoder, 1);
164
165 if (!p_schro_params->decoder)
166 return -1;
167
168 /* Initialize the decoded frame queue. */
169 ff_schro_queue_init(&p_schro_params->dec_frame_queue);
170 return 0;
171 }
172
173 static void libschroedinger_decode_frame_free(void *frame)
174 {
175 schro_frame_unref(frame);
176 }
177
178 static void libschroedinger_handle_first_access_unit(AVCodecContext *avccontext)
179 {
180 SchroDecoderParams *p_schro_params = avccontext->priv_data;
181 SchroDecoder *decoder = p_schro_params->decoder;
182
183 p_schro_params->format = schro_decoder_get_video_format(decoder);
184
185 /* Tell Libav about sequence details. */
186 if (av_image_check_size(p_schro_params->format->width,
187 p_schro_params->format->height, 0, avccontext) < 0) {
188 av_log(avccontext, AV_LOG_ERROR, "invalid dimensions (%dx%d)\n",
189 p_schro_params->format->width, p_schro_params->format->height);
190 avccontext->height = avccontext->width = 0;
191 return;
192 }
193 avccontext->height = p_schro_params->format->height;
194 avccontext->width = p_schro_params->format->width;
195 avccontext->pix_fmt = get_chroma_format(p_schro_params->format->chroma_format);
196
197 if (ff_get_schro_frame_format(p_schro_params->format->chroma_format,
198 &p_schro_params->frame_format) == -1) {
199 av_log(avccontext, AV_LOG_ERROR,
200 "This codec currently only supports planar YUV 4:2:0, 4:2:2 "
201 "and 4:4:4 formats.\n");
202 return;
203 }
204
205 avccontext->time_base.den = p_schro_params->format->frame_rate_numerator;
206 avccontext->time_base.num = p_schro_params->format->frame_rate_denominator;
207 }
208
209 static int libschroedinger_decode_frame(AVCodecContext *avccontext,
210 void *data, int *data_size,
211 AVPacket *avpkt)
212 {
213 const uint8_t *buf = avpkt->data;
214 int buf_size = avpkt->size;
215 int64_t pts = avpkt->pts;
216 SchroTag *tag;
217
218 SchroDecoderParams *p_schro_params = avccontext->priv_data;
219 SchroDecoder *decoder = p_schro_params->decoder;
220 SchroBuffer *enc_buf;
221 SchroFrame* frame;
222 int state;
223 int go = 1;
224 int outer = 1;
225 SchroParseUnitContext parse_ctx;
226 LibSchroFrameContext *framewithpts = NULL;
227
228 *data_size = 0;
229
230 parse_context_init(&parse_ctx, buf, buf_size);
231 if (!buf_size) {
232 if (!p_schro_params->eos_signalled) {
233 state = schro_decoder_push_end_of_stream(decoder);
234 p_schro_params->eos_signalled = 1;
235 }
236 }
237
238 /* Loop through all the individual parse units in the input buffer */
239 do {
240 if ((enc_buf = find_next_parse_unit(&parse_ctx))) {
241 /* Set Schrotag with the pts to be recovered after decoding*/
242 enc_buf->tag = schro_tag_new(av_malloc(sizeof(int64_t)), av_free);
243 if (!enc_buf->tag->value) {
244 av_log(avccontext, AV_LOG_ERROR, "Unable to allocate SchroTag\n");
245 return AVERROR(ENOMEM);
246 }
247 AV_WN(64, enc_buf->tag->value, pts);
248 /* Push buffer into decoder. */
249 if (SCHRO_PARSE_CODE_IS_PICTURE(enc_buf->data[4]) &&
250 SCHRO_PARSE_CODE_NUM_REFS(enc_buf->data[4]) > 0)
251 avccontext->has_b_frames = 1;
252 state = schro_decoder_push(decoder, enc_buf);
253 if (state == SCHRO_DECODER_FIRST_ACCESS_UNIT)
254 libschroedinger_handle_first_access_unit(avccontext);
255 go = 1;
256 } else
257 outer = 0;
258
259 while (go) {
260 /* Parse data and process result. */
261 state = schro_decoder_wait(decoder);
262 switch (state) {
263 case SCHRO_DECODER_FIRST_ACCESS_UNIT:
264 libschroedinger_handle_first_access_unit(avccontext);
265 break;
266
267 case SCHRO_DECODER_NEED_BITS:
268 /* Need more input data - stop iterating over what we have. */
269 go = 0;
270 break;
271
272 case SCHRO_DECODER_NEED_FRAME:
273 /* Decoder needs a frame - create one and push it in. */
274 frame = ff_create_schro_frame(avccontext,
275 p_schro_params->frame_format);
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(avccontext, 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 (p_schro_params->dec_frame.data[0])
316 avccontext->release_buffer(avccontext, &p_schro_params->dec_frame);
317 if (ff_get_buffer(avccontext, &p_schro_params->dec_frame) < 0) {
318 av_log(avccontext, AV_LOG_ERROR, "Unable to allocate buffer\n");
319 return AVERROR(ENOMEM);
320 }
321
322 memcpy(p_schro_params->dec_frame.data[0],
323 framewithpts->frame->components[0].data,
324 framewithpts->frame->components[0].length);
325
326 memcpy(p_schro_params->dec_frame.data[1],
327 framewithpts->frame->components[1].data,
328 framewithpts->frame->components[1].length);
329
330 memcpy(p_schro_params->dec_frame.data[2],
331 framewithpts->frame->components[2].data,
332 framewithpts->frame->components[2].length);
333
334 /* Fill frame with current buffer data from Schroedinger. */
335 p_schro_params->dec_frame.format = -1; /* Unknown -1 */
336 p_schro_params->dec_frame.width = framewithpts->frame->width;
337 p_schro_params->dec_frame.height = framewithpts->frame->height;
338 p_schro_params->dec_frame.pkt_pts = framewithpts->pts;
339 p_schro_params->dec_frame.linesize[0] = framewithpts->frame->components[0].stride;
340 p_schro_params->dec_frame.linesize[1] = framewithpts->frame->components[1].stride;
341 p_schro_params->dec_frame.linesize[2] = framewithpts->frame->components[2].stride;
342
343 *(AVFrame*)data = p_schro_params->dec_frame;
344 *data_size = sizeof(AVFrame);
345
346 /* Now free the frame resources. */
347 libschroedinger_decode_frame_free(framewithpts->frame);
348 av_free(framewithpts);
349 } else {
350 data = NULL;
351 *data_size = 0;
352 }
353 return buf_size;
354 }
355
356
357 static av_cold int libschroedinger_decode_close(AVCodecContext *avccontext)
358 {
359 SchroDecoderParams *p_schro_params = avccontext->priv_data;
360 /* Free the decoder. */
361 schro_decoder_free(p_schro_params->decoder);
362 av_freep(&p_schro_params->format);
363
364 if (p_schro_params->dec_frame.data[0])
365 avccontext->release_buffer(avccontext, &p_schro_params->dec_frame);
366
367 /* Free data in the output frame queue. */
368 ff_schro_queue_free(&p_schro_params->dec_frame_queue,
369 libschroedinger_decode_frame_free);
370
371 return 0;
372 }
373
374 static void libschroedinger_flush(AVCodecContext *avccontext)
375 {
376 /* Got a seek request. Free the decoded frames queue and then reset
377 * the decoder */
378 SchroDecoderParams *p_schro_params = avccontext->priv_data;
379
380 /* Free data in the output frame queue. */
381 ff_schro_queue_free(&p_schro_params->dec_frame_queue,
382 libschroedinger_decode_frame_free);
383
384 ff_schro_queue_init(&p_schro_params->dec_frame_queue);
385 schro_decoder_reset(p_schro_params->decoder);
386 p_schro_params->eos_pulled = 0;
387 p_schro_params->eos_signalled = 0;
388 }
389
390 AVCodec ff_libschroedinger_decoder = {
391 .name = "libschroedinger",
392 .type = AVMEDIA_TYPE_VIDEO,
393 .id = AV_CODEC_ID_DIRAC,
394 .priv_data_size = sizeof(SchroDecoderParams),
395 .init = libschroedinger_decode_init,
396 .close = libschroedinger_decode_close,
397 .decode = libschroedinger_decode_frame,
398 .capabilities = CODEC_CAP_DELAY,
399 .flush = libschroedinger_flush,
400 .long_name = NULL_IF_CONFIG_SMALL("libschroedinger Dirac 2.2"),
401 };