proresdec.c
Go to the documentation of this file.
1 /*
2  * Apple ProRes compatible decoder
3  *
4  * Copyright (c) 2010-2011 Maxim Poliakovski
5  *
6  * This file is part of Libav.
7  *
8  * Libav is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU Lesser General Public
10  * License as published by the Free Software Foundation; either
11  * version 2.1 of the License, or (at your option) any later version.
12  *
13  * Libav is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16  * Lesser General Public License for more details.
17  *
18  * You should have received a copy of the GNU Lesser General Public
19  * License along with Libav; if not, write to the Free Software
20  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
21  */
22 
31 #define LONG_BITSTREAM_READER // some ProRes vlc codes require up to 28 bits to be read at once
32 
33 #include <stdint.h>
34 
35 #include "libavutil/intmath.h"
36 #include "avcodec.h"
37 #include "internal.h"
38 #include "proresdata.h"
39 #include "proresdsp.h"
40 #include "get_bits.h"
41 
42 typedef struct {
43  const uint8_t *index;
44  int slice_num;
45  int x_pos, y_pos;
48  DECLARE_ALIGNED(16, DCTELEM, blocks)[8 * 4 * 64];
49  DECLARE_ALIGNED(16, int16_t, qmat_luma_scaled)[64];
50  DECLARE_ALIGNED(16, int16_t, qmat_chroma_scaled)[64];
52 
53 typedef struct {
58 
59  int frame_type;
60  int pic_format;
61  uint8_t qmat_luma[64];
62  uint8_t qmat_chroma[64];
66  int pic_num;
74  int num_x_mbs;
75  int num_y_mbs;
78 
79 
81 {
82  ProresContext *ctx = avctx->priv_data;
83 
84  ctx->total_slices = 0;
85  ctx->slice_data = NULL;
86 
88  ff_proresdsp_init(&ctx->dsp);
89 
90  avctx->coded_frame = &ctx->picture;
93  ctx->picture.key_frame = 1;
94 
95  ctx->scantable_type = -1; // set scantable type to uninitialized
96  memset(ctx->qmat_luma, 4, 64);
97  memset(ctx->qmat_chroma, 4, 64);
98 
99  return 0;
100 }
101 
102 
103 static int decode_frame_header(ProresContext *ctx, const uint8_t *buf,
104  const int data_size, AVCodecContext *avctx)
105 {
106  int hdr_size, version, width, height, flags;
107  const uint8_t *ptr;
108 
109  hdr_size = AV_RB16(buf);
110  if (hdr_size > data_size) {
111  av_log(avctx, AV_LOG_ERROR, "frame data too small\n");
112  return AVERROR_INVALIDDATA;
113  }
114 
115  version = AV_RB16(buf + 2);
116  if (version >= 2) {
117  av_log(avctx, AV_LOG_ERROR,
118  "unsupported header version: %d\n", version);
119  return AVERROR_INVALIDDATA;
120  }
121 
122  width = AV_RB16(buf + 8);
123  height = AV_RB16(buf + 10);
124  if (width != avctx->width || height != avctx->height) {
125  av_log(avctx, AV_LOG_ERROR,
126  "picture dimension changed: old: %d x %d, new: %d x %d\n",
127  avctx->width, avctx->height, width, height);
128  return AVERROR_INVALIDDATA;
129  }
130 
131  ctx->frame_type = (buf[12] >> 2) & 3;
132  if (ctx->frame_type > 2) {
133  av_log(avctx, AV_LOG_ERROR,
134  "unsupported frame type: %d\n", ctx->frame_type);
135  return AVERROR_INVALIDDATA;
136  }
137 
138  ctx->chroma_factor = (buf[12] >> 6) & 3;
139  ctx->mb_chroma_factor = ctx->chroma_factor + 2;
140  ctx->num_chroma_blocks = (1 << ctx->chroma_factor) >> 1;
141  switch (ctx->chroma_factor) {
142  case 2:
143  avctx->pix_fmt = AV_PIX_FMT_YUV422P10;
144  break;
145  case 3:
146  avctx->pix_fmt = AV_PIX_FMT_YUV444P10;
147  break;
148  default:
149  av_log(avctx, AV_LOG_ERROR,
150  "unsupported picture format: %d\n", ctx->pic_format);
151  return AVERROR_INVALIDDATA;
152  }
153 
154  if (ctx->scantable_type != ctx->frame_type) {
155  if (!ctx->frame_type)
158  else
161  ctx->scantable_type = ctx->frame_type;
162  }
163 
164  if (ctx->frame_type) { /* if interlaced */
165  ctx->picture.interlaced_frame = 1;
166  ctx->picture.top_field_first = ctx->frame_type & 1;
167  } else {
168  ctx->picture.interlaced_frame = 0;
169  }
170 
171  avctx->color_primaries = buf[14];
172  avctx->color_trc = buf[15];
173  avctx->colorspace = buf[16];
174 
175  ctx->alpha_info = buf[17] & 0xf;
176  if (ctx->alpha_info)
177  av_log_missing_feature(avctx, "Alpha channel", 0);
178 
179  ctx->qmat_changed = 0;
180  ptr = buf + 20;
181  flags = buf[19];
182  if (flags & 2) {
183  if (ptr - buf > hdr_size - 64) {
184  av_log(avctx, AV_LOG_ERROR, "header data too small\n");
185  return AVERROR_INVALIDDATA;
186  }
187  if (memcmp(ctx->qmat_luma, ptr, 64)) {
188  memcpy(ctx->qmat_luma, ptr, 64);
189  ctx->qmat_changed = 1;
190  }
191  ptr += 64;
192  } else {
193  memset(ctx->qmat_luma, 4, 64);
194  ctx->qmat_changed = 1;
195  }
196 
197  if (flags & 1) {
198  if (ptr - buf > hdr_size - 64) {
199  av_log(avctx, AV_LOG_ERROR, "header data too small\n");
200  return -1;
201  }
202  if (memcmp(ctx->qmat_chroma, ptr, 64)) {
203  memcpy(ctx->qmat_chroma, ptr, 64);
204  ctx->qmat_changed = 1;
205  }
206  } else {
207  memset(ctx->qmat_chroma, 4, 64);
208  ctx->qmat_changed = 1;
209  }
210 
211  return hdr_size;
212 }
213 
214 
215 static int decode_picture_header(ProresContext *ctx, const uint8_t *buf,
216  const int data_size, AVCodecContext *avctx)
217 {
218  int i, hdr_size, pic_data_size, num_slices;
219  int slice_width_factor, slice_height_factor;
220  int remainder, num_x_slices;
221  const uint8_t *data_ptr, *index_ptr;
222 
223  hdr_size = data_size > 0 ? buf[0] >> 3 : 0;
224  if (hdr_size < 8 || hdr_size > data_size) {
225  av_log(avctx, AV_LOG_ERROR, "picture header too small\n");
226  return AVERROR_INVALIDDATA;
227  }
228 
229  pic_data_size = AV_RB32(buf + 1);
230  if (pic_data_size > data_size) {
231  av_log(avctx, AV_LOG_ERROR, "picture data too small\n");
232  return AVERROR_INVALIDDATA;
233  }
234 
235  slice_width_factor = buf[7] >> 4;
236  slice_height_factor = buf[7] & 0xF;
237  if (slice_width_factor > 3 || slice_height_factor) {
238  av_log(avctx, AV_LOG_ERROR,
239  "unsupported slice dimension: %d x %d\n",
240  1 << slice_width_factor, 1 << slice_height_factor);
241  return AVERROR_INVALIDDATA;
242  }
243 
244  ctx->slice_width_factor = slice_width_factor;
245  ctx->slice_height_factor = slice_height_factor;
246 
247  ctx->num_x_mbs = (avctx->width + 15) >> 4;
248  ctx->num_y_mbs = (avctx->height +
249  (1 << (4 + ctx->picture.interlaced_frame)) - 1) >>
250  (4 + ctx->picture.interlaced_frame);
251 
252  remainder = ctx->num_x_mbs & ((1 << slice_width_factor) - 1);
253  num_x_slices = (ctx->num_x_mbs >> slice_width_factor) + (remainder & 1) +
254  ((remainder >> 1) & 1) + ((remainder >> 2) & 1);
255 
256  num_slices = num_x_slices * ctx->num_y_mbs;
257  if (num_slices != AV_RB16(buf + 5)) {
258  av_log(avctx, AV_LOG_ERROR, "invalid number of slices\n");
259  return AVERROR_INVALIDDATA;
260  }
261 
262  if (ctx->total_slices != num_slices) {
263  av_freep(&ctx->slice_data);
264  ctx->slice_data = av_malloc((num_slices + 1) * sizeof(ctx->slice_data[0]));
265  if (!ctx->slice_data)
266  return AVERROR(ENOMEM);
267  ctx->total_slices = num_slices;
268  }
269 
270  if (hdr_size + num_slices * 2 > data_size) {
271  av_log(avctx, AV_LOG_ERROR, "slice table too small\n");
272  return AVERROR_INVALIDDATA;
273  }
274 
275  /* parse slice table allowing quick access to the slice data */
276  index_ptr = buf + hdr_size;
277  data_ptr = index_ptr + num_slices * 2;
278 
279  for (i = 0; i < num_slices; i++) {
280  ctx->slice_data[i].index = data_ptr;
281  ctx->slice_data[i].prev_slice_sf = 0;
282  data_ptr += AV_RB16(index_ptr + i * 2);
283  }
284  ctx->slice_data[i].index = data_ptr;
285  ctx->slice_data[i].prev_slice_sf = 0;
286 
287  if (data_ptr > buf + data_size) {
288  av_log(avctx, AV_LOG_ERROR, "out of slice data\n");
289  return -1;
290  }
291 
292  return pic_data_size;
293 }
294 
295 
299 static inline int decode_vlc_codeword(GetBitContext *gb, unsigned codebook)
300 {
301  unsigned int rice_order, exp_order, switch_bits;
302  unsigned int buf, code;
303  int log, prefix_len, len;
304 
305  OPEN_READER(re, gb);
306  UPDATE_CACHE(re, gb);
307  buf = GET_CACHE(re, gb);
308 
309  /* number of prefix bits to switch between Rice and expGolomb */
310  switch_bits = (codebook & 3) + 1;
311  rice_order = codebook >> 5; /* rice code order */
312  exp_order = (codebook >> 2) & 7; /* exp golomb code order */
313 
314  log = 31 - av_log2(buf); /* count prefix bits (zeroes) */
315 
316  if (log < switch_bits) { /* ok, we got a rice code */
317  if (!rice_order) {
318  /* shortcut for faster decoding of rice codes without remainder */
319  code = log;
320  LAST_SKIP_BITS(re, gb, log + 1);
321  } else {
322  prefix_len = log + 1;
323  code = (log << rice_order) + NEG_USR32(buf << prefix_len, rice_order);
324  LAST_SKIP_BITS(re, gb, prefix_len + rice_order);
325  }
326  } else { /* otherwise we got a exp golomb code */
327  len = (log << 1) - switch_bits + exp_order + 1;
328  code = NEG_USR32(buf, len) - (1 << exp_order) + (switch_bits << rice_order);
329  LAST_SKIP_BITS(re, gb, len);
330  }
331 
332  CLOSE_READER(re, gb);
333 
334  return code;
335 }
336 
337 #define LSB2SIGN(x) (-((x) & 1))
338 #define TOSIGNED(x) (((x) >> 1) ^ LSB2SIGN(x))
339 
343 static inline void decode_dc_coeffs(GetBitContext *gb, DCTELEM *out,
344  int nblocks)
345 {
346  DCTELEM prev_dc;
347  int i, sign;
348  int16_t delta;
349  unsigned int code;
350 
351  code = decode_vlc_codeword(gb, FIRST_DC_CB);
352  out[0] = prev_dc = TOSIGNED(code);
353 
354  out += 64; /* move to the DC coeff of the next block */
355  delta = 3;
356 
357  for (i = 1; i < nblocks; i++, out += 64) {
358  code = decode_vlc_codeword(gb, ff_prores_dc_codebook[FFMIN(FFABS(delta), 3)]);
359 
360  sign = -(((delta >> 15) & 1) ^ (code & 1));
361  delta = (((code + 1) >> 1) ^ sign) - sign;
362  prev_dc += delta;
363  out[0] = prev_dc;
364  }
365 }
366 
367 
371 static inline void decode_ac_coeffs(GetBitContext *gb, DCTELEM *out,
372  int blocks_per_slice,
373  int plane_size_factor,
374  const uint8_t *scan)
375 {
376  int pos, block_mask, run, level, sign, run_cb_index, lev_cb_index;
377  int max_coeffs, bits_left;
378 
379  /* set initial prediction values */
380  run = 4;
381  level = 2;
382 
383  max_coeffs = blocks_per_slice << 6;
384  block_mask = blocks_per_slice - 1;
385 
386  for (pos = blocks_per_slice - 1; pos < max_coeffs;) {
387  run_cb_index = ff_prores_run_to_cb_index[FFMIN(run, 15)];
388  lev_cb_index = ff_prores_lev_to_cb_index[FFMIN(level, 9)];
389 
390  bits_left = get_bits_left(gb);
391  if (bits_left <= 0 || (bits_left <= 8 && !show_bits(gb, bits_left)))
392  return;
393 
394  run = decode_vlc_codeword(gb, ff_prores_ac_codebook[run_cb_index]);
395 
396  bits_left = get_bits_left(gb);
397  if (bits_left <= 0 || (bits_left <= 8 && !show_bits(gb, bits_left)))
398  return;
399 
400  level = decode_vlc_codeword(gb, ff_prores_ac_codebook[lev_cb_index]) + 1;
401 
402  pos += run + 1;
403  if (pos >= max_coeffs)
404  break;
405 
406  sign = get_sbits(gb, 1);
407  out[((pos & block_mask) << 6) + scan[pos >> plane_size_factor]] =
408  (level ^ sign) - sign;
409  }
410 }
411 
412 
417  const uint8_t *buf,
418  int data_size, uint16_t *out_ptr,
419  int linesize, int mbs_per_slice,
420  int blocks_per_mb, int plane_size_factor,
421  const int16_t *qmat, int is_chroma)
422 {
423  GetBitContext gb;
424  DCTELEM *block_ptr;
425  int mb_num, blocks_per_slice;
426 
427  blocks_per_slice = mbs_per_slice * blocks_per_mb;
428 
429  memset(td->blocks, 0, 8 * 4 * 64 * sizeof(*td->blocks));
430 
431  init_get_bits(&gb, buf, data_size << 3);
432 
433  decode_dc_coeffs(&gb, td->blocks, blocks_per_slice);
434 
435  decode_ac_coeffs(&gb, td->blocks, blocks_per_slice,
436  plane_size_factor, ctx->scantable.permutated);
437 
438  /* inverse quantization, inverse transform and output */
439  block_ptr = td->blocks;
440 
441  if (!is_chroma) {
442  for (mb_num = 0; mb_num < mbs_per_slice; mb_num++, out_ptr += blocks_per_mb * 4) {
443  ctx->dsp.idct_put(out_ptr, linesize, block_ptr, qmat);
444  block_ptr += 64;
445  if (blocks_per_mb > 2) {
446  ctx->dsp.idct_put(out_ptr + 8, linesize, block_ptr, qmat);
447  block_ptr += 64;
448  }
449  ctx->dsp.idct_put(out_ptr + linesize * 4, linesize, block_ptr, qmat);
450  block_ptr += 64;
451  if (blocks_per_mb > 2) {
452  ctx->dsp.idct_put(out_ptr + linesize * 4 + 8, linesize, block_ptr, qmat);
453  block_ptr += 64;
454  }
455  }
456  } else {
457  for (mb_num = 0; mb_num < mbs_per_slice; mb_num++, out_ptr += blocks_per_mb * 4) {
458  ctx->dsp.idct_put(out_ptr, linesize, block_ptr, qmat);
459  block_ptr += 64;
460  ctx->dsp.idct_put(out_ptr + linesize * 4, linesize, block_ptr, qmat);
461  block_ptr += 64;
462  if (blocks_per_mb > 2) {
463  ctx->dsp.idct_put(out_ptr + 8, linesize, block_ptr, qmat);
464  block_ptr += 64;
465  ctx->dsp.idct_put(out_ptr + linesize * 4 + 8, linesize, block_ptr, qmat);
466  block_ptr += 64;
467  }
468  }
469  }
470 }
471 
472 
473 static int decode_slice(AVCodecContext *avctx, void *tdata)
474 {
475  ProresThreadData *td = tdata;
476  ProresContext *ctx = avctx->priv_data;
477  int mb_x_pos = td->x_pos;
478  int mb_y_pos = td->y_pos;
479  int pic_num = ctx->pic_num;
480  int slice_num = td->slice_num;
481  int mbs_per_slice = td->slice_width;
482  const uint8_t *buf;
483  uint8_t *y_data, *u_data, *v_data;
484  AVFrame *pic = avctx->coded_frame;
485  int i, sf, slice_width_factor;
486  int slice_data_size, hdr_size, y_data_size, u_data_size, v_data_size;
487  int y_linesize, u_linesize, v_linesize;
488 
489  buf = ctx->slice_data[slice_num].index;
490  slice_data_size = ctx->slice_data[slice_num + 1].index - buf;
491 
492  slice_width_factor = av_log2(mbs_per_slice);
493 
494  y_data = pic->data[0];
495  u_data = pic->data[1];
496  v_data = pic->data[2];
497  y_linesize = pic->linesize[0];
498  u_linesize = pic->linesize[1];
499  v_linesize = pic->linesize[2];
500 
501  if (pic->interlaced_frame) {
502  if (!(pic_num ^ pic->top_field_first)) {
503  y_data += y_linesize;
504  u_data += u_linesize;
505  v_data += v_linesize;
506  }
507  y_linesize <<= 1;
508  u_linesize <<= 1;
509  v_linesize <<= 1;
510  }
511 
512  if (slice_data_size < 6) {
513  av_log(avctx, AV_LOG_ERROR, "slice data too small\n");
514  return AVERROR_INVALIDDATA;
515  }
516 
517  /* parse slice header */
518  hdr_size = buf[0] >> 3;
519  y_data_size = AV_RB16(buf + 2);
520  u_data_size = AV_RB16(buf + 4);
521  v_data_size = hdr_size > 7 ? AV_RB16(buf + 6) :
522  slice_data_size - y_data_size - u_data_size - hdr_size;
523 
524  if (hdr_size + y_data_size + u_data_size + v_data_size > slice_data_size ||
525  v_data_size < 0 || hdr_size < 6) {
526  av_log(avctx, AV_LOG_ERROR, "invalid data size\n");
527  return AVERROR_INVALIDDATA;
528  }
529 
530  sf = av_clip(buf[1], 1, 224);
531  sf = sf > 128 ? (sf - 96) << 2 : sf;
532 
533  /* scale quantization matrixes according with slice's scale factor */
534  /* TODO: this can be SIMD-optimized a lot */
535  if (ctx->qmat_changed || sf != td->prev_slice_sf) {
536  td->prev_slice_sf = sf;
537  for (i = 0; i < 64; i++) {
538  td->qmat_luma_scaled[ctx->dsp.idct_permutation[i]] = ctx->qmat_luma[i] * sf;
539  td->qmat_chroma_scaled[ctx->dsp.idct_permutation[i]] = ctx->qmat_chroma[i] * sf;
540  }
541  }
542 
543  /* decode luma plane */
544  decode_slice_plane(ctx, td, buf + hdr_size, y_data_size,
545  (uint16_t*) (y_data + (mb_y_pos << 4) * y_linesize +
546  (mb_x_pos << 5)), y_linesize,
547  mbs_per_slice, 4, slice_width_factor + 2,
548  td->qmat_luma_scaled, 0);
549 
550  /* decode U chroma plane */
551  decode_slice_plane(ctx, td, buf + hdr_size + y_data_size, u_data_size,
552  (uint16_t*) (u_data + (mb_y_pos << 4) * u_linesize +
553  (mb_x_pos << ctx->mb_chroma_factor)),
554  u_linesize, mbs_per_slice, ctx->num_chroma_blocks,
555  slice_width_factor + ctx->chroma_factor - 1,
556  td->qmat_chroma_scaled, 1);
557 
558  /* decode V chroma plane */
559  decode_slice_plane(ctx, td, buf + hdr_size + y_data_size + u_data_size,
560  v_data_size,
561  (uint16_t*) (v_data + (mb_y_pos << 4) * v_linesize +
562  (mb_x_pos << ctx->mb_chroma_factor)),
563  v_linesize, mbs_per_slice, ctx->num_chroma_blocks,
564  slice_width_factor + ctx->chroma_factor - 1,
565  td->qmat_chroma_scaled, 1);
566 
567  return 0;
568 }
569 
570 
571 static int decode_picture(ProresContext *ctx, int pic_num,
572  AVCodecContext *avctx)
573 {
574  int slice_num, slice_width, x_pos, y_pos;
575 
576  slice_num = 0;
577 
578  ctx->pic_num = pic_num;
579  for (y_pos = 0; y_pos < ctx->num_y_mbs; y_pos++) {
580  slice_width = 1 << ctx->slice_width_factor;
581 
582  for (x_pos = 0; x_pos < ctx->num_x_mbs && slice_width;
583  x_pos += slice_width) {
584  while (ctx->num_x_mbs - x_pos < slice_width)
585  slice_width >>= 1;
586 
587  ctx->slice_data[slice_num].slice_num = slice_num;
588  ctx->slice_data[slice_num].x_pos = x_pos;
589  ctx->slice_data[slice_num].y_pos = y_pos;
590  ctx->slice_data[slice_num].slice_width = slice_width;
591 
592  slice_num++;
593  }
594  }
595 
596  return avctx->execute(avctx, decode_slice,
597  ctx->slice_data, NULL, slice_num,
598  sizeof(ctx->slice_data[0]));
599 }
600 
601 
602 #define MOVE_DATA_PTR(nbytes) buf += (nbytes); buf_size -= (nbytes)
603 
604 static int decode_frame(AVCodecContext *avctx, void *data, int *got_frame,
605  AVPacket *avpkt)
606 {
607  ProresContext *ctx = avctx->priv_data;
608  AVFrame *picture = avctx->coded_frame;
609  const uint8_t *buf = avpkt->data;
610  int buf_size = avpkt->size;
611  int frame_hdr_size, pic_num, pic_data_size;
612 
613  /* check frame atom container */
614  if (buf_size < 28 || buf_size < AV_RB32(buf) ||
615  AV_RB32(buf + 4) != FRAME_ID) {
616  av_log(avctx, AV_LOG_ERROR, "invalid frame\n");
617  return AVERROR_INVALIDDATA;
618  }
619 
620  MOVE_DATA_PTR(8);
621 
622  frame_hdr_size = decode_frame_header(ctx, buf, buf_size, avctx);
623  if (frame_hdr_size < 0)
624  return AVERROR_INVALIDDATA;
625 
626  MOVE_DATA_PTR(frame_hdr_size);
627 
628  if (picture->data[0])
629  avctx->release_buffer(avctx, picture);
630 
631  picture->reference = 0;
632  if (ff_get_buffer(avctx, picture) < 0)
633  return -1;
634 
635  for (pic_num = 0; ctx->picture.interlaced_frame - pic_num + 1; pic_num++) {
636  pic_data_size = decode_picture_header(ctx, buf, buf_size, avctx);
637  if (pic_data_size < 0)
638  return AVERROR_INVALIDDATA;
639 
640  if (decode_picture(ctx, pic_num, avctx))
641  return -1;
642 
643  MOVE_DATA_PTR(pic_data_size);
644  }
645 
646  *got_frame = 1;
647  *(AVFrame*) data = *avctx->coded_frame;
648 
649  return avpkt->size;
650 }
651 
652 
654 {
655  ProresContext *ctx = avctx->priv_data;
656 
657  if (ctx->picture.data[0])
658  avctx->release_buffer(avctx, &ctx->picture);
659 
660  av_freep(&ctx->slice_data);
661 
662  return 0;
663 }
664 
665 
667  .name = "prores",
668  .type = AVMEDIA_TYPE_VIDEO,
669  .id = AV_CODEC_ID_PRORES,
670  .priv_data_size = sizeof(ProresContext),
671  .init = decode_init,
672  .close = decode_close,
673  .decode = decode_frame,
674  .capabilities = CODEC_CAP_DR1 | CODEC_CAP_SLICE_THREADS,
675  .long_name = NULL_IF_CONFIG_SMALL("Apple ProRes (iCodec Pro)")
676 };