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 
73 
77 
78 typedef struct SchroParseUnitContext {
79  const uint8_t *buf;
80  int buf_size;
82 
83 
84 static void libschroedinger_decode_buffer_free(SchroBuffer *schro_buf,
85  void *priv)
86 {
87  av_freep(&priv);
88 }
89 
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 
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 
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 
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 
210  void *data, int *got_frame,
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  *got_frame = 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)
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:
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  *got_frame = 1;
345 
346  /* Now free the frame resources. */
348  av_free(framewithpts);
349  } else {
350  data = NULL;
351  *got_frame = 0;
352  }
353  return buf_size;
354 }
355 
356 
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,
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,
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 
391  .name = "libschroedinger",
392  .type = AVMEDIA_TYPE_VIDEO,
393  .id = AV_CODEC_ID_DIRAC,
394  .priv_data_size = sizeof(SchroDecoderParams),
398  .capabilities = CODEC_CAP_DELAY,
400  .long_name = NULL_IF_CONFIG_SMALL("libschroedinger Dirac 2.2"),
401 };
void * av_malloc(size_t size)
Allocate a block of size bytes with alignment suitable for all memory accesses (including vectors if ...
Definition: mem.c:61
This structure describes decoded (raw) audio or video data.
Definition: avcodec.h:989
static av_cold int libschroedinger_decode_close(AVCodecContext *avccontext)
struct LibSchroFrameContext LibSchroFrameContext
SchroFrame and Pts relation.
void(* release_buffer)(struct AVCodecContext *c, AVFrame *pic)
Called to release buffers which were allocated with get_buffer.
Definition: avcodec.h:2259
misc image utilities
SchroChromaFormat schro_pix_fmt
memory handling functions
SchroVideoFormat * format
Schroedinger video format.
int num
numerator
Definition: rational.h:44
int size
Definition: avcodec.h:916
static const struct @34 schro_pixel_format_map[]
enum AVPixelFormat pix_fmt
Pixel format, see AV_PIX_FMT_xxx.
Definition: avcodec.h:1533
static SchroBuffer * find_next_parse_unit(SchroParseUnitContext *parse_ctx)
static enum AVPixelFormat get_chroma_format(SchroChromaFormat schro_pix_fmt)
Returns Libav chroma format.
data structures common to libschroedinger decoder and encoder
AVCodec.
Definition: avcodec.h:2960
static void libschroedinger_decode_buffer_free(SchroBuffer *schro_buf, void *priv)
SchroFrameFormat frame_format
Schroedinger frame format.
AVRational time_base
This is the fundamental unit of time (in seconds) in terms of which frame timestamps are represented...
Definition: avcodec.h:1465
void av_freep(void *arg)
Free a memory block which has been allocated with av_malloc(z)() or av_realloc() and set the pointer ...
Definition: mem.c:151
static int decode(MimicContext *ctx, int quality, int num_coeffs, int is_iframe)
Definition: mimic.c:228
uint8_t
SchroFrame * ff_create_schro_frame(AVCodecContext *avccontext, SchroFrameFormat schro_frame_fmt)
Create a Schro frame based on the dimensions and frame format passed.
#define AV_WN(s, p, v)
Definition: intreadwrite.h:316
static void libschroedinger_flush(AVCodecContext *avccontext)
const char data[16]
Definition: mxf.c:66
SchroDecoder * decoder
decoder handle
uint8_t * data
Definition: avcodec.h:915
uint32_t tag
Definition: movenc.c:802
static int init(AVCodecParserContext *s)
Definition: h264_parser.c:335
struct SchroDecoderParams SchroDecoderParams
libschroedinger decoder private data
static void libschroedinger_handle_first_access_unit(AVCodecContext *avccontext)
int width
width and height of the video frame
Definition: avcodec.h:1035
int has_b_frames
Size of the frame reordering buffer in the decoder.
Definition: avcodec.h:1634
void av_free(void *ptr)
Free a memory block which has been allocated with av_malloc(z)() or av_realloc(). ...
Definition: mem.c:139
void * ff_schro_queue_pop(FFSchroQueue *queue)
Return the first element in the queue.
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification. ...
Definition: internal.h:88
void ff_schro_queue_init(FFSchroQueue *queue)
Initialise the queue.
AVCodec ff_libschroedinger_decoder
void av_log(void *avcl, int level, const char *fmt,...)
Definition: log.c:146
const char * name
Name of the codec implementation.
Definition: avcodec.h:2967
int ff_get_schro_frame_format(SchroChromaFormat schro_pix_fmt, SchroFrameFormat *schro_frame_fmt)
Sets the Schroedinger frame format corresponding to the Schro chroma format passed.
common internal API header
int av_image_check_size(unsigned int w, unsigned int h, int log_offset, void *log_ctx)
Check if the given dimension of an image is valid, meaning that all bytes of the image can be address...
Definition: imgutils.c:220
int ff_schro_queue_push_back(FFSchroQueue *queue, void *p_data)
Add an element to the end of the queue.
void ff_schro_queue_free(FFSchroQueue *queue, void(*free_func)(void *))
Free the queue resources.
A simple queue implementation used in libschroedinger.
static const chunk_decoder decoder[8]
Definition: dfa.c:303
int width
picture width / height.
Definition: avcodec.h:1508
int ff_get_buffer(AVCodecContext *avctx, AVFrame *frame)
Get a buffer for a frame.
Definition: utils.c:464
libschroedinger decoder private data
static av_cold int libschroedinger_decode_init(AVCodecContext *avccontext)
SchroFrame and Pts relation.
int format
format of the frame, -1 if unknown or unset Values correspond to enum AVPixelFormat for video frames...
Definition: avcodec.h:1051
NULL
Definition: eval.c:52
external API header
int linesize[AV_NUM_DATA_POINTERS]
Size, in bytes, of the data for each picture/channel plane.
Definition: avcodec.h:1008
int debug
debug
Definition: avcodec.h:2568
main external API structure.
Definition: avcodec.h:1339
static void close(AVCodecParserContext *s)
Definition: h264_parser.c:326
int eos_signalled
end of sequence signalled
FFSchroQueue dec_frame_queue
queue storing decoded frames
int64_t pkt_pts
pts copied from the AVPacket that was decoded to produce this frame
Definition: avcodec.h:1095
static uint32_t state
Definition: trasher.c:27
int eos_pulled
end of sequence pulled
static void parse_context_init(SchroParseUnitContext *parse_ctx, const uint8_t *buf, int buf_size)
uint8_t * data[AV_NUM_DATA_POINTERS]
pointer to the picture/channel planes.
Definition: avcodec.h:997
common internal api header.
static av_cold void flush(AVCodecContext *avctx)
Flush (reset) the frame ID after seeking.
Definition: alsdec.c:1772
int den
denominator
Definition: rational.h:45
void * priv_data
Definition: avcodec.h:1382
static int libschroedinger_decode_frame(AVCodecContext *avccontext, void *data, int *got_frame, AVPacket *avpkt)
#define AV_RN64(p)
Definition: intreadwrite.h:330
struct SchroParseUnitContext SchroParseUnitContext
int height
Definition: avcodec.h:1035
static void libschroedinger_decode_frame_free(void *frame)
AVPixelFormat
Pixel format.
Definition: pixfmt.h:63
This structure stores compressed data.
Definition: avcodec.h:898
AVFrame dec_frame
decoded picture
int64_t pts
Presentation timestamp in AVStream->time_base units; the time at which the decompressed packet will b...
Definition: avcodec.h:908
if(!(ptr_align%ac->ptr_align)&&samples_align >=aligned_len)