Libav
libschroedingerdec.c
Go to the documentation of this file.
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 
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 
49 typedef struct LibSchroFrameContext {
50  SchroFrame *frame;
51  int64_t pts;
53 
55 typedef struct SchroDecoderParams {
57  SchroVideoFormat *format;
58 
60  SchroFrameFormat frame_format;
61 
63  SchroDecoder* decoder;
64 
67 
70 
74 
75 typedef struct SchroParseUnitContext {
76  const uint8_t *buf;
77  int buf_size;
79 
80 
81 static void libschroedinger_decode_buffer_free(SchroBuffer *schro_buf,
82  void *priv)
83 {
84  av_freep(&priv);
85 }
86 
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 
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 
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 
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->time_base.den = p_schro_params->format->frame_rate_numerator;
203  avctx->time_base.num = p_schro_params->format->frame_rate_denominator;
204 }
205 
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)
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:
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  schro_decoder_add_output_picture(decoder, frame);
275  break;
276 
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);
281 
282  if (frame) {
283  /* Add relation between schroframe and pts. */
284  framewithpts = av_malloc(sizeof(LibSchroFrameContext));
285  if (!framewithpts) {
286  av_log(avctx, AV_LOG_ERROR, "Unable to allocate FrameWithPts\n");
287  return AVERROR(ENOMEM);
288  }
289  framewithpts->frame = frame;
290  framewithpts->pts = AV_RN64(tag->value);
291  ff_schro_queue_push_back(&p_schro_params->dec_frame_queue,
292  framewithpts);
293  }
294  break;
295  case SCHRO_DECODER_EOS:
296  go = 0;
297  p_schro_params->eos_pulled = 1;
298  schro_decoder_reset(decoder);
299  outer = 0;
300  break;
301 
302  case SCHRO_DECODER_ERROR:
303  return -1;
304  break;
305  }
306  }
307  } while (outer);
308 
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);
311 
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);
316  }
317 
318  memcpy(avframe->data[0],
319  framewithpts->frame->components[0].data,
320  framewithpts->frame->components[0].length);
321 
322  memcpy(avframe->data[1],
323  framewithpts->frame->components[1].data,
324  framewithpts->frame->components[1].length);
325 
326  memcpy(avframe->data[2],
327  framewithpts->frame->components[2].data,
328  framewithpts->frame->components[2].length);
329 
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;
335 
336  *got_frame = 1;
337 
338  /* Now free the frame resources. */
340  av_free(framewithpts);
341  } else {
342  data = NULL;
343  *got_frame = 0;
344  }
345  return buf_size;
346 }
347 
348 
350 {
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);
355 
356  /* Free data in the output frame queue. */
357  ff_schro_queue_free(&p_schro_params->dec_frame_queue,
359 
360  return 0;
361 }
362 
364 {
365  /* Got a seek request. Free the decoded frames queue and then reset
366  * the decoder */
367  SchroDecoderParams *p_schro_params = avctx->priv_data;
368 
369  /* Free data in the output frame queue. */
370  ff_schro_queue_free(&p_schro_params->dec_frame_queue,
372 
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;
377 }
378 
380  .name = "libschroedinger",
381  .long_name = NULL_IF_CONFIG_SMALL("libschroedinger Dirac 2.2"),
382  .type = AVMEDIA_TYPE_VIDEO,
383  .id = AV_CODEC_ID_DIRAC,
384  .priv_data_size = sizeof(SchroDecoderParams),
388  .capabilities = CODEC_CAP_DELAY,
390 };