FFmpeg
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
utvideodec.c
Go to the documentation of this file.
1 /*
2  * Ut Video decoder
3  * Copyright (c) 2011 Konstantin Shishkov
4  *
5  * This file is part of FFmpeg.
6  *
7  * FFmpeg 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  * FFmpeg 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 FFmpeg; 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  * Ut Video decoder
25  */
26 
27 #include <inttypes.h>
28 #include <stdlib.h>
29 
30 #include "libavutil/intreadwrite.h"
31 #include "avcodec.h"
32 #include "bswapdsp.h"
33 #include "bytestream.h"
34 #include "get_bits.h"
35 #include "thread.h"
36 #include "utvideo.h"
37 
38 static int build_huff10(const uint8_t *src, VLC *vlc, int *fsym)
39 {
40  int i;
41  HuffEntry he[1024];
42  int last;
43  uint32_t codes[1024];
44  uint8_t bits[1024];
45  uint16_t syms[1024];
46  uint32_t code;
47 
48  *fsym = -1;
49  for (i = 0; i < 1024; i++) {
50  he[i].sym = i;
51  he[i].len = *src++;
52  }
53  qsort(he, 1024, sizeof(*he), ff_ut10_huff_cmp_len);
54 
55  if (!he[0].len) {
56  *fsym = he[0].sym;
57  return 0;
58  }
59 
60  last = 1023;
61  while (he[last].len == 255 && last)
62  last--;
63 
64  if (he[last].len > 32) {
65  return -1;
66  }
67 
68  code = 1;
69  for (i = last; i >= 0; i--) {
70  codes[i] = code >> (32 - he[i].len);
71  bits[i] = he[i].len;
72  syms[i] = he[i].sym;
73  code += 0x80000000u >> (he[i].len - 1);
74  }
75 
76  return ff_init_vlc_sparse(vlc, FFMIN(he[last].len, 11), last + 1,
77  bits, sizeof(*bits), sizeof(*bits),
78  codes, sizeof(*codes), sizeof(*codes),
79  syms, sizeof(*syms), sizeof(*syms), 0);
80 }
81 
82 static int build_huff(const uint8_t *src, VLC *vlc, int *fsym)
83 {
84  int i;
85  HuffEntry he[256];
86  int last;
87  uint32_t codes[256];
88  uint8_t bits[256];
89  uint8_t syms[256];
90  uint32_t code;
91 
92  *fsym = -1;
93  for (i = 0; i < 256; i++) {
94  he[i].sym = i;
95  he[i].len = *src++;
96  }
97  qsort(he, 256, sizeof(*he), ff_ut_huff_cmp_len);
98 
99  if (!he[0].len) {
100  *fsym = he[0].sym;
101  return 0;
102  }
103 
104  last = 255;
105  while (he[last].len == 255 && last)
106  last--;
107 
108  if (he[last].len > 32)
109  return -1;
110 
111  code = 1;
112  for (i = last; i >= 0; i--) {
113  codes[i] = code >> (32 - he[i].len);
114  bits[i] = he[i].len;
115  syms[i] = he[i].sym;
116  code += 0x80000000u >> (he[i].len - 1);
117  }
118 
119  return ff_init_vlc_sparse(vlc, FFMIN(he[last].len, 11), last + 1,
120  bits, sizeof(*bits), sizeof(*bits),
121  codes, sizeof(*codes), sizeof(*codes),
122  syms, sizeof(*syms), sizeof(*syms), 0);
123 }
124 
125 static int decode_plane10(UtvideoContext *c, int plane_no,
126  uint16_t *dst, int step, int stride,
127  int width, int height,
128  const uint8_t *src, const uint8_t *huff,
129  int use_pred)
130 {
131  int i, j, slice, pix, ret;
132  int sstart, send;
133  VLC vlc;
134  GetBitContext gb;
135  int prev, fsym;
136 
137  if ((ret = build_huff10(huff, &vlc, &fsym)) < 0) {
138  av_log(c->avctx, AV_LOG_ERROR, "Cannot build Huffman codes\n");
139  return ret;
140  }
141  if (fsym >= 0) { // build_huff reported a symbol to fill slices with
142  send = 0;
143  for (slice = 0; slice < c->slices; slice++) {
144  uint16_t *dest;
145 
146  sstart = send;
147  send = (height * (slice + 1) / c->slices);
148  dest = dst + sstart * stride;
149 
150  prev = 0x200;
151  for (j = sstart; j < send; j++) {
152  for (i = 0; i < width * step; i += step) {
153  pix = fsym;
154  if (use_pred) {
155  prev += pix;
156  prev &= 0x3FF;
157  pix = prev;
158  }
159  dest[i] = pix;
160  }
161  dest += stride;
162  }
163  }
164  return 0;
165  }
166 
167  send = 0;
168  for (slice = 0; slice < c->slices; slice++) {
169  uint16_t *dest;
170  int slice_data_start, slice_data_end, slice_size;
171 
172  sstart = send;
173  send = (height * (slice + 1) / c->slices);
174  dest = dst + sstart * stride;
175 
176  // slice offset and size validation was done earlier
177  slice_data_start = slice ? AV_RL32(src + slice * 4 - 4) : 0;
178  slice_data_end = AV_RL32(src + slice * 4);
179  slice_size = slice_data_end - slice_data_start;
180 
181  if (!slice_size) {
182  av_log(c->avctx, AV_LOG_ERROR, "Plane has more than one symbol "
183  "yet a slice has a length of zero.\n");
184  goto fail;
185  }
186 
187  memcpy(c->slice_bits, src + slice_data_start + c->slices * 4,
188  slice_size);
189  memset(c->slice_bits + slice_size, 0, AV_INPUT_BUFFER_PADDING_SIZE);
190  c->bdsp.bswap_buf((uint32_t *) c->slice_bits,
191  (uint32_t *) c->slice_bits,
192  (slice_data_end - slice_data_start + 3) >> 2);
193  init_get_bits(&gb, c->slice_bits, slice_size * 8);
194 
195  prev = 0x200;
196  for (j = sstart; j < send; j++) {
197  for (i = 0; i < width * step; i += step) {
198  if (get_bits_left(&gb) <= 0) {
200  "Slice decoding ran out of bits\n");
201  goto fail;
202  }
203  pix = get_vlc2(&gb, vlc.table, vlc.bits, 3);
204  if (pix < 0) {
205  av_log(c->avctx, AV_LOG_ERROR, "Decoding error\n");
206  goto fail;
207  }
208  if (use_pred) {
209  prev += pix;
210  prev &= 0x3FF;
211  pix = prev;
212  }
213  dest[i] = pix;
214  }
215  dest += stride;
216  }
217  if (get_bits_left(&gb) > 32)
219  "%d bits left after decoding slice\n", get_bits_left(&gb));
220  }
221 
222  ff_free_vlc(&vlc);
223 
224  return 0;
225 fail:
226  ff_free_vlc(&vlc);
227  return AVERROR_INVALIDDATA;
228 }
229 
230 static int decode_plane(UtvideoContext *c, int plane_no,
231  uint8_t *dst, int step, int stride,
232  int width, int height,
233  const uint8_t *src, int use_pred)
234 {
235  int i, j, slice, pix;
236  int sstart, send;
237  VLC vlc;
238  GetBitContext gb;
239  int prev, fsym;
240  const int cmask = ~(!plane_no && c->avctx->pix_fmt == AV_PIX_FMT_YUV420P);
241 
242  if (build_huff(src, &vlc, &fsym)) {
243  av_log(c->avctx, AV_LOG_ERROR, "Cannot build Huffman codes\n");
244  return AVERROR_INVALIDDATA;
245  }
246  if (fsym >= 0) { // build_huff reported a symbol to fill slices with
247  send = 0;
248  for (slice = 0; slice < c->slices; slice++) {
249  uint8_t *dest;
250 
251  sstart = send;
252  send = (height * (slice + 1) / c->slices) & cmask;
253  dest = dst + sstart * stride;
254 
255  prev = 0x80;
256  for (j = sstart; j < send; j++) {
257  for (i = 0; i < width * step; i += step) {
258  pix = fsym;
259  if (use_pred) {
260  prev += pix;
261  pix = prev;
262  }
263  dest[i] = pix;
264  }
265  dest += stride;
266  }
267  }
268  return 0;
269  }
270 
271  src += 256;
272 
273  send = 0;
274  for (slice = 0; slice < c->slices; slice++) {
275  uint8_t *dest;
276  int slice_data_start, slice_data_end, slice_size;
277 
278  sstart = send;
279  send = (height * (slice + 1) / c->slices) & cmask;
280  dest = dst + sstart * stride;
281 
282  // slice offset and size validation was done earlier
283  slice_data_start = slice ? AV_RL32(src + slice * 4 - 4) : 0;
284  slice_data_end = AV_RL32(src + slice * 4);
285  slice_size = slice_data_end - slice_data_start;
286 
287  if (!slice_size) {
288  av_log(c->avctx, AV_LOG_ERROR, "Plane has more than one symbol "
289  "yet a slice has a length of zero.\n");
290  goto fail;
291  }
292 
293  memcpy(c->slice_bits, src + slice_data_start + c->slices * 4,
294  slice_size);
295  memset(c->slice_bits + slice_size, 0, AV_INPUT_BUFFER_PADDING_SIZE);
296  c->bdsp.bswap_buf((uint32_t *) c->slice_bits,
297  (uint32_t *) c->slice_bits,
298  (slice_data_end - slice_data_start + 3) >> 2);
299  init_get_bits(&gb, c->slice_bits, slice_size * 8);
300 
301  prev = 0x80;
302  for (j = sstart; j < send; j++) {
303  for (i = 0; i < width * step; i += step) {
304  if (get_bits_left(&gb) <= 0) {
306  "Slice decoding ran out of bits\n");
307  goto fail;
308  }
309  pix = get_vlc2(&gb, vlc.table, vlc.bits, 3);
310  if (pix < 0) {
311  av_log(c->avctx, AV_LOG_ERROR, "Decoding error\n");
312  goto fail;
313  }
314  if (use_pred) {
315  prev += pix;
316  pix = prev;
317  }
318  dest[i] = pix;
319  }
320  dest += stride;
321  }
322  if (get_bits_left(&gb) > 32)
324  "%d bits left after decoding slice\n", get_bits_left(&gb));
325  }
326 
327  ff_free_vlc(&vlc);
328 
329  return 0;
330 fail:
331  ff_free_vlc(&vlc);
332  return AVERROR_INVALIDDATA;
333 }
334 
335 static void restore_rgb_planes(uint8_t *src, int step, int stride, int width,
336  int height)
337 {
338  int i, j;
339  uint8_t r, g, b;
340 
341  for (j = 0; j < height; j++) {
342  for (i = 0; i < width * step; i += step) {
343  r = src[i];
344  g = src[i + 1];
345  b = src[i + 2];
346  src[i] = r + g - 0x80;
347  src[i + 2] = b + g - 0x80;
348  }
349  src += stride;
350  }
351 }
352 
354 {
355  uint16_t *src_r = (uint16_t *)frame->data[2];
356  uint16_t *src_g = (uint16_t *)frame->data[0];
357  uint16_t *src_b = (uint16_t *)frame->data[1];
358  int r, g, b;
359  int i, j;
360 
361  for (j = 0; j < height; j++) {
362  for (i = 0; i < width; i++) {
363  r = src_r[i];
364  g = src_g[i];
365  b = src_b[i];
366  src_r[i] = (r + g - 0x200) & 0x3FF;
367  src_b[i] = (b + g - 0x200) & 0x3FF;
368  }
369  src_r += frame->linesize[2] / 2;
370  src_g += frame->linesize[0] / 2;
371  src_b += frame->linesize[1] / 2;
372  }
373 }
374 
375 static void restore_median(uint8_t *src, int step, int stride,
376  int width, int height, int slices, int rmode)
377 {
378  int i, j, slice;
379  int A, B, C;
380  uint8_t *bsrc;
381  int slice_start, slice_height;
382  const int cmask = ~rmode;
383 
384  for (slice = 0; slice < slices; slice++) {
385  slice_start = ((slice * height) / slices) & cmask;
386  slice_height = ((((slice + 1) * height) / slices) & cmask) -
387  slice_start;
388 
389  if (!slice_height)
390  continue;
391  bsrc = src + slice_start * stride;
392 
393  // first line - left neighbour prediction
394  bsrc[0] += 0x80;
395  A = bsrc[0];
396  for (i = step; i < width * step; i += step) {
397  bsrc[i] += A;
398  A = bsrc[i];
399  }
400  bsrc += stride;
401  if (slice_height <= 1)
402  continue;
403  // second line - first element has top prediction, the rest uses median
404  C = bsrc[-stride];
405  bsrc[0] += C;
406  A = bsrc[0];
407  for (i = step; i < width * step; i += step) {
408  B = bsrc[i - stride];
409  bsrc[i] += mid_pred(A, B, (uint8_t)(A + B - C));
410  C = B;
411  A = bsrc[i];
412  }
413  bsrc += stride;
414  // the rest of lines use continuous median prediction
415  for (j = 2; j < slice_height; j++) {
416  for (i = 0; i < width * step; i += step) {
417  B = bsrc[i - stride];
418  bsrc[i] += mid_pred(A, B, (uint8_t)(A + B - C));
419  C = B;
420  A = bsrc[i];
421  }
422  bsrc += stride;
423  }
424  }
425 }
426 
427 /* UtVideo interlaced mode treats every two lines as a single one,
428  * so restoring function should take care of possible padding between
429  * two parts of the same "line".
430  */
431 static void restore_median_il(uint8_t *src, int step, int stride,
432  int width, int height, int slices, int rmode)
433 {
434  int i, j, slice;
435  int A, B, C;
436  uint8_t *bsrc;
437  int slice_start, slice_height;
438  const int cmask = ~(rmode ? 3 : 1);
439  const int stride2 = stride << 1;
440 
441  for (slice = 0; slice < slices; slice++) {
442  slice_start = ((slice * height) / slices) & cmask;
443  slice_height = ((((slice + 1) * height) / slices) & cmask) -
444  slice_start;
445  slice_height >>= 1;
446  if (!slice_height)
447  continue;
448 
449  bsrc = src + slice_start * stride;
450 
451  // first line - left neighbour prediction
452  bsrc[0] += 0x80;
453  A = bsrc[0];
454  for (i = step; i < width * step; i += step) {
455  bsrc[i] += A;
456  A = bsrc[i];
457  }
458  for (i = 0; i < width * step; i += step) {
459  bsrc[stride + i] += A;
460  A = bsrc[stride + i];
461  }
462  bsrc += stride2;
463  if (slice_height <= 1)
464  continue;
465  // second line - first element has top prediction, the rest uses median
466  C = bsrc[-stride2];
467  bsrc[0] += C;
468  A = bsrc[0];
469  for (i = step; i < width * step; i += step) {
470  B = bsrc[i - stride2];
471  bsrc[i] += mid_pred(A, B, (uint8_t)(A + B - C));
472  C = B;
473  A = bsrc[i];
474  }
475  for (i = 0; i < width * step; i += step) {
476  B = bsrc[i - stride];
477  bsrc[stride + i] += mid_pred(A, B, (uint8_t)(A + B - C));
478  C = B;
479  A = bsrc[stride + i];
480  }
481  bsrc += stride2;
482  // the rest of lines use continuous median prediction
483  for (j = 2; j < slice_height; j++) {
484  for (i = 0; i < width * step; i += step) {
485  B = bsrc[i - stride2];
486  bsrc[i] += mid_pred(A, B, (uint8_t)(A + B - C));
487  C = B;
488  A = bsrc[i];
489  }
490  for (i = 0; i < width * step; i += step) {
491  B = bsrc[i - stride];
492  bsrc[i + stride] += mid_pred(A, B, (uint8_t)(A + B - C));
493  C = B;
494  A = bsrc[i + stride];
495  }
496  bsrc += stride2;
497  }
498  }
499 }
500 
501 static int decode_frame(AVCodecContext *avctx, void *data, int *got_frame,
502  AVPacket *avpkt)
503 {
504  const uint8_t *buf = avpkt->data;
505  int buf_size = avpkt->size;
506  UtvideoContext *c = avctx->priv_data;
507  int i, j;
508  const uint8_t *plane_start[5];
509  int plane_size, max_slice_size = 0, slice_start, slice_end, slice_size;
510  int ret;
511  GetByteContext gb;
512  ThreadFrame frame = { .f = data };
513 
514  if ((ret = ff_thread_get_buffer(avctx, &frame, 0)) < 0)
515  return ret;
516 
517  /* parse plane structure to get frame flags and validate slice offsets */
518  bytestream2_init(&gb, buf, buf_size);
519  if (c->pro) {
521  av_log(avctx, AV_LOG_ERROR, "Not enough data for frame information\n");
522  return AVERROR_INVALIDDATA;
523  }
524  c->frame_info = bytestream2_get_le32u(&gb);
525  c->slices = ((c->frame_info >> 16) & 0xff) + 1;
526  for (i = 0; i < c->planes; i++) {
527  plane_start[i] = gb.buffer;
528  if (bytestream2_get_bytes_left(&gb) < 1024 + 4 * c->slices) {
529  av_log(avctx, AV_LOG_ERROR, "Insufficient data for a plane\n");
530  return AVERROR_INVALIDDATA;
531  }
532  slice_start = 0;
533  slice_end = 0;
534  for (j = 0; j < c->slices; j++) {
535  slice_end = bytestream2_get_le32u(&gb);
536  if (slice_end < 0 || slice_end < slice_start ||
537  bytestream2_get_bytes_left(&gb) < slice_end) {
538  av_log(avctx, AV_LOG_ERROR, "Incorrect slice size\n");
539  return AVERROR_INVALIDDATA;
540  }
541  slice_size = slice_end - slice_start;
542  slice_start = slice_end;
543  max_slice_size = FFMAX(max_slice_size, slice_size);
544  }
545  plane_size = slice_end;
546  bytestream2_skipu(&gb, plane_size);
547  bytestream2_skipu(&gb, 1024);
548  }
549  plane_start[c->planes] = gb.buffer;
550  } else {
551  for (i = 0; i < c->planes; i++) {
552  plane_start[i] = gb.buffer;
553  if (bytestream2_get_bytes_left(&gb) < 256 + 4 * c->slices) {
554  av_log(avctx, AV_LOG_ERROR, "Insufficient data for a plane\n");
555  return AVERROR_INVALIDDATA;
556  }
557  bytestream2_skipu(&gb, 256);
558  slice_start = 0;
559  slice_end = 0;
560  for (j = 0; j < c->slices; j++) {
561  slice_end = bytestream2_get_le32u(&gb);
562  if (slice_end < 0 || slice_end < slice_start ||
563  bytestream2_get_bytes_left(&gb) < slice_end) {
564  av_log(avctx, AV_LOG_ERROR, "Incorrect slice size\n");
565  return AVERROR_INVALIDDATA;
566  }
567  slice_size = slice_end - slice_start;
568  slice_start = slice_end;
569  max_slice_size = FFMAX(max_slice_size, slice_size);
570  }
571  plane_size = slice_end;
572  bytestream2_skipu(&gb, plane_size);
573  }
574  plane_start[c->planes] = gb.buffer;
576  av_log(avctx, AV_LOG_ERROR, "Not enough data for frame information\n");
577  return AVERROR_INVALIDDATA;
578  }
579  c->frame_info = bytestream2_get_le32u(&gb);
580  }
581  av_log(avctx, AV_LOG_DEBUG, "frame information flags %"PRIX32"\n",
582  c->frame_info);
583 
584  c->frame_pred = (c->frame_info >> 8) & 3;
585 
586  if (c->frame_pred == PRED_GRADIENT) {
587  avpriv_request_sample(avctx, "Frame with gradient prediction");
588  return AVERROR_PATCHWELCOME;
589  }
590 
592  max_slice_size + AV_INPUT_BUFFER_PADDING_SIZE);
593 
594  if (!c->slice_bits) {
595  av_log(avctx, AV_LOG_ERROR, "Cannot allocate temporary buffer\n");
596  return AVERROR(ENOMEM);
597  }
598 
599  switch (c->avctx->pix_fmt) {
600  case AV_PIX_FMT_RGB24:
601  case AV_PIX_FMT_RGBA:
602  for (i = 0; i < c->planes; i++) {
603  ret = decode_plane(c, i, frame.f->data[0] + ff_ut_rgb_order[i],
604  c->planes, frame.f->linesize[0], avctx->width,
605  avctx->height, plane_start[i],
606  c->frame_pred == PRED_LEFT);
607  if (ret)
608  return ret;
609  if (c->frame_pred == PRED_MEDIAN) {
610  if (!c->interlaced) {
611  restore_median(frame.f->data[0] + ff_ut_rgb_order[i],
612  c->planes, frame.f->linesize[0], avctx->width,
613  avctx->height, c->slices, 0);
614  } else {
615  restore_median_il(frame.f->data[0] + ff_ut_rgb_order[i],
616  c->planes, frame.f->linesize[0],
617  avctx->width, avctx->height, c->slices,
618  0);
619  }
620  }
621  }
622  restore_rgb_planes(frame.f->data[0], c->planes, frame.f->linesize[0],
623  avctx->width, avctx->height);
624  break;
625  case AV_PIX_FMT_GBRAP10:
626  case AV_PIX_FMT_GBRP10:
627  for (i = 0; i < c->planes; i++) {
628  ret = decode_plane10(c, i, (uint16_t *)frame.f->data[i], 1,
629  frame.f->linesize[i] / 2, avctx->width,
630  avctx->height, plane_start[i],
631  plane_start[i + 1] - 1024,
632  c->frame_pred == PRED_LEFT);
633  if (ret)
634  return ret;
635  }
636  restore_rgb_planes10(frame.f, avctx->width, avctx->height);
637  break;
638  case AV_PIX_FMT_YUV420P:
639  for (i = 0; i < 3; i++) {
640  ret = decode_plane(c, i, frame.f->data[i], 1, frame.f->linesize[i],
641  avctx->width >> !!i, avctx->height >> !!i,
642  plane_start[i], c->frame_pred == PRED_LEFT);
643  if (ret)
644  return ret;
645  if (c->frame_pred == PRED_MEDIAN) {
646  if (!c->interlaced) {
647  restore_median(frame.f->data[i], 1, frame.f->linesize[i],
648  avctx->width >> !!i, avctx->height >> !!i,
649  c->slices, !i);
650  } else {
651  restore_median_il(frame.f->data[i], 1, frame.f->linesize[i],
652  avctx->width >> !!i,
653  avctx->height >> !!i,
654  c->slices, !i);
655  }
656  }
657  }
658  break;
659  case AV_PIX_FMT_YUV422P:
660  for (i = 0; i < 3; i++) {
661  ret = decode_plane(c, i, frame.f->data[i], 1, frame.f->linesize[i],
662  avctx->width >> !!i, avctx->height,
663  plane_start[i], c->frame_pred == PRED_LEFT);
664  if (ret)
665  return ret;
666  if (c->frame_pred == PRED_MEDIAN) {
667  if (!c->interlaced) {
668  restore_median(frame.f->data[i], 1, frame.f->linesize[i],
669  avctx->width >> !!i, avctx->height,
670  c->slices, 0);
671  } else {
672  restore_median_il(frame.f->data[i], 1, frame.f->linesize[i],
673  avctx->width >> !!i, avctx->height,
674  c->slices, 0);
675  }
676  }
677  }
678  break;
680  for (i = 0; i < 3; i++) {
681  ret = decode_plane10(c, i, (uint16_t *)frame.f->data[i], 1, frame.f->linesize[i] / 2,
682  avctx->width >> !!i, avctx->height,
683  plane_start[i], plane_start[i + 1] - 1024, c->frame_pred == PRED_LEFT);
684  if (ret)
685  return ret;
686  }
687  break;
688  }
689 
690  frame.f->key_frame = 1;
691  frame.f->pict_type = AV_PICTURE_TYPE_I;
692  frame.f->interlaced_frame = !!c->interlaced;
693 
694  *got_frame = 1;
695 
696  /* always report that the buffer was completely consumed */
697  return buf_size;
698 }
699 
701 {
702  UtvideoContext * const c = avctx->priv_data;
703 
704  c->avctx = avctx;
705 
706  ff_bswapdsp_init(&c->bdsp);
707 
708  if (avctx->extradata_size >= 16) {
709  av_log(avctx, AV_LOG_DEBUG, "Encoder version %d.%d.%d.%d\n",
710  avctx->extradata[3], avctx->extradata[2],
711  avctx->extradata[1], avctx->extradata[0]);
712  av_log(avctx, AV_LOG_DEBUG, "Original format %"PRIX32"\n",
713  AV_RB32(avctx->extradata + 4));
714  c->frame_info_size = AV_RL32(avctx->extradata + 8);
715  c->flags = AV_RL32(avctx->extradata + 12);
716 
717  if (c->frame_info_size != 4)
718  avpriv_request_sample(avctx, "Frame info not 4 bytes");
719  av_log(avctx, AV_LOG_DEBUG, "Encoding parameters %08"PRIX32"\n", c->flags);
720  c->slices = (c->flags >> 24) + 1;
721  c->compression = c->flags & 1;
722  c->interlaced = c->flags & 0x800;
723  } else if (avctx->extradata_size == 8) {
724  av_log(avctx, AV_LOG_DEBUG, "Encoder version %d.%d.%d.%d\n",
725  avctx->extradata[3], avctx->extradata[2],
726  avctx->extradata[1], avctx->extradata[0]);
727  av_log(avctx, AV_LOG_DEBUG, "Original format %"PRIX32"\n",
728  AV_RB32(avctx->extradata + 4));
729  c->interlaced = 0;
730  c->pro = 1;
731  c->frame_info_size = 4;
732  } else {
733  av_log(avctx, AV_LOG_ERROR,
734  "Insufficient extradata size %d, should be at least 16\n",
735  avctx->extradata_size);
736  return AVERROR_INVALIDDATA;
737  }
738 
739  c->slice_bits_size = 0;
740 
741  switch (avctx->codec_tag) {
742  case MKTAG('U', 'L', 'R', 'G'):
743  c->planes = 3;
744  avctx->pix_fmt = AV_PIX_FMT_RGB24;
745  break;
746  case MKTAG('U', 'L', 'R', 'A'):
747  c->planes = 4;
748  avctx->pix_fmt = AV_PIX_FMT_RGBA;
749  break;
750  case MKTAG('U', 'L', 'Y', '0'):
751  c->planes = 3;
752  avctx->pix_fmt = AV_PIX_FMT_YUV420P;
753  avctx->colorspace = AVCOL_SPC_BT470BG;
754  break;
755  case MKTAG('U', 'L', 'Y', '2'):
756  c->planes = 3;
757  avctx->pix_fmt = AV_PIX_FMT_YUV422P;
758  avctx->colorspace = AVCOL_SPC_BT470BG;
759  break;
760  case MKTAG('U', 'Q', 'Y', '2'):
761  c->planes = 3;
762  avctx->pix_fmt = AV_PIX_FMT_YUV422P10;
763  break;
764  case MKTAG('U', 'Q', 'R', 'G'):
765  c->planes = 3;
766  avctx->pix_fmt = AV_PIX_FMT_GBRP10;
767  break;
768  case MKTAG('U', 'Q', 'R', 'A'):
769  c->planes = 4;
770  avctx->pix_fmt = AV_PIX_FMT_GBRAP10;
771  break;
772  case MKTAG('U', 'L', 'H', '0'):
773  c->planes = 3;
774  avctx->pix_fmt = AV_PIX_FMT_YUV420P;
775  avctx->colorspace = AVCOL_SPC_BT709;
776  break;
777  case MKTAG('U', 'L', 'H', '2'):
778  c->planes = 3;
779  avctx->pix_fmt = AV_PIX_FMT_YUV422P;
780  avctx->colorspace = AVCOL_SPC_BT709;
781  break;
782  default:
783  av_log(avctx, AV_LOG_ERROR, "Unknown Ut Video FOURCC provided (%08X)\n",
784  avctx->codec_tag);
785  return AVERROR_INVALIDDATA;
786  }
787 
788  return 0;
789 }
790 
792 {
793  UtvideoContext * const c = avctx->priv_data;
794 
795  av_freep(&c->slice_bits);
796 
797  return 0;
798 }
799 
801  .name = "utvideo",
802  .long_name = NULL_IF_CONFIG_SMALL("Ut Video"),
803  .type = AVMEDIA_TYPE_VIDEO,
804  .id = AV_CODEC_ID_UTVIDEO,
805  .priv_data_size = sizeof(UtvideoContext),
806  .init = decode_init,
807  .close = decode_end,
808  .decode = decode_frame,
810 };
static void restore_median(uint8_t *src, int step, int stride, int width, int height, int slices, int rmode)
Definition: utvideodec.c:375
void(* bswap_buf)(uint32_t *dst, const uint32_t *src, int w)
Definition: bswapdsp.h:25
also ITU-R BT1361 / IEC 61966-2-4 xvYCC709 / SMPTE RP177 Annex B
Definition: pixfmt.h:436
#define AVERROR_INVALIDDATA
Invalid data found when processing input.
Definition: error.h:59
This structure describes decoded (raw) audio or video data.
Definition: frame.h:184
int ff_ut10_huff_cmp_len(const void *a, const void *b)
Definition: utvideo.c:43
ptrdiff_t const GLvoid * data
Definition: opengl_enc.c:101
#define C
#define AV_PIX_FMT_GBRAP10
Definition: pixfmt.h:361
uint32_t flags
Definition: utvideo.h:73
#define AV_LOG_WARNING
Something somehow does not look correct.
Definition: log.h:182
AVFrame * f
Definition: thread.h:36
packed RGB 8:8:8, 24bpp, RGBRGB...
Definition: pixfmt.h:64
const char * g
Definition: vf_curves.c:108
static av_cold int init(AVCodecContext *avctx)
Definition: avrndec.c:35
also ITU-R BT601-6 625 / ITU-R BT1358 625 / ITU-R BT1700 625 PAL & SECAM / IEC 61966-2-4 xvYCC601 ...
Definition: pixfmt.h:440
int slice_bits_size
Definition: utvideo.h:83
int ff_init_vlc_sparse(VLC *vlc_arg, int nb_bits, int nb_codes, const void *bits, int bits_wrap, int bits_size, const void *codes, int codes_wrap, int codes_size, const void *symbols, int symbols_wrap, int symbols_size, int flags)
Definition: bitstream.c:275
int size
Definition: avcodec.h:1581
const char * b
Definition: vf_curves.c:109
#define AV_PIX_FMT_GBRP10
Definition: pixfmt.h:357
static int decode_plane(UtvideoContext *c, int plane_no, uint8_t *dst, int step, int stride, int width, int height, const uint8_t *src, int use_pred)
Definition: utvideodec.c:230
static av_cold int decode_end(AVCodecContext *avctx)
Definition: utvideodec.c:791
enum AVPixelFormat pix_fmt
Pixel format, see AV_PIX_FMT_xxx.
Definition: avcodec.h:1877
static av_always_inline void bytestream2_init(GetByteContext *g, const uint8_t *buf, int buf_size)
Definition: bytestream.h:133
AVCodec.
Definition: avcodec.h:3542
int interlaced
Definition: utvideo.h:77
void void avpriv_request_sample(void *avc, const char *msg,...) av_printf_format(2
Log a generic warning message about a missing feature.
uint8_t bits
Definition: crc.c:296
uint8_t
#define av_cold
Definition: attributes.h:82
Multithreading support functions.
Definition: vf_geq.c:46
uint8_t * extradata
some codecs need / can use extradata like Huffman tables.
Definition: avcodec.h:1764
uint64_t_TMPL AV_WL64 unsigned int_TMPL AV_WL32 unsigned int_TMPL AV_WL24 unsigned int_TMPL AV_WL16 uint64_t_TMPL AV_WB64 unsigned int_TMPL AV_RB32
Definition: bytestream.h:87
uint32_t frame_info
Definition: utvideo.h:73
static void restore_median_il(uint8_t *src, int step, int stride, int width, int height, int slices, int rmode)
Definition: utvideodec.c:431
static AVFrame * frame
#define height
uint8_t * data
Definition: avcodec.h:1580
const uint8_t * buffer
Definition: bytestream.h:34
static av_always_inline void bytestream2_skipu(GetByteContext *g, unsigned int size)
Definition: bytestream.h:170
bitstream reader API header.
int interlaced_frame
The content of the picture is interlaced.
Definition: frame.h:318
#define A(x)
Definition: vp56_arith.h:28
const int ff_ut_rgb_order[4]
Definition: utvideo.c:35
#define av_log(a,...)
static int build_huff(const uint8_t *src, VLC *vlc, int *fsym)
Definition: utvideodec.c:82
BswapDSPContext bdsp
Definition: utvideo.h:70
static int get_bits_left(GetBitContext *gb)
Definition: get_bits.h:568
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:176
static av_cold int decode_init(AVCodecContext *avctx)
Definition: utvideodec.c:700
#define AVERROR(e)
Definition: error.h:43
uint8_t sym
Definition: magicyuv.c:70
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification. ...
Definition: internal.h:176
AVCodecContext * avctx
Definition: utvideo.h:69
const char * r
Definition: vf_curves.c:107
#define AV_LOG_DEBUG
Stuff which is only useful for libav* developers.
Definition: log.h:197
static av_always_inline unsigned int bytestream2_get_bytes_left(GetByteContext *g)
Definition: bytestream.h:154
const char * name
Name of the codec implementation.
Definition: avcodec.h:3549
uint32_t frame_info_size
Definition: utvideo.h:73
#define FFMAX(a, b)
Definition: common.h:94
#define fail()
Definition: checkasm.h:81
#define AV_CODEC_CAP_FRAME_THREADS
Codec supports frame-level multithreading.
Definition: avcodec.h:1019
packed RGBA 8:8:8:8, 32bpp, RGBARGBA...
Definition: pixfmt.h:94
Definition: vlc.h:26
planar YUV 4:2:2, 16bpp, (1 Cr & Cb sample per 2x1 Y samples)
Definition: pixfmt.h:66
int compression
Definition: utvideo.h:76
static int decode_plane10(UtvideoContext *c, int plane_no, uint16_t *dst, int step, int stride, int width, int height, const uint8_t *src, const uint8_t *huff, int use_pred)
Definition: utvideodec.c:125
enum AVPictureType pict_type
Picture type of the frame.
Definition: frame.h:258
#define FFMIN(a, b)
Definition: common.h:96
#define width
int width
picture width / height.
Definition: avcodec.h:1836
static av_always_inline int get_vlc2(GetBitContext *s, VLC_TYPE(*table)[2], int bits, int max_depth)
Parse a vlc code.
Definition: get_bits.h:535
#define src
Definition: vp9dsp.c:530
static void restore_rgb_planes10(AVFrame *frame, int width, int height)
Definition: utvideodec.c:353
int bits
Definition: vlc.h:27
#define AVERROR_PATCHWELCOME
Not yet implemented in FFmpeg, patches welcome.
Definition: error.h:62
static void restore_rgb_planes(uint8_t *src, int step, int stride, int width, int height)
Definition: utvideodec.c:335
static int decode_frame(AVCodecContext *avctx, void *data, int *got_frame, AVPacket *avpkt)
Definition: utvideodec.c:501
Common Ut Video header.
int frame_pred
Definition: utvideo.h:78
uint8_t len
Definition: magicyuv.c:71
Libavcodec external API header.
int linesize[AV_NUM_DATA_POINTERS]
For video, size in bytes of each picture line.
Definition: frame.h:215
int ff_thread_get_buffer(AVCodecContext *avctx, ThreadFrame *f, int flags)
Wrapper around get_buffer() for frame-multithreaded codecs.
main external API structure.
Definition: avcodec.h:1649
unsigned int codec_tag
fourcc (LSB first, so "ABCD" -> ('D'<<24) + ('C'<<16) + ('B'<<8) + 'A').
Definition: avcodec.h:1681
void * buf
Definition: avisynth_c.h:553
int extradata_size
Definition: avcodec.h:1765
enum AVColorSpace colorspace
YUV colorspace type.
Definition: avcodec.h:2378
static int init_get_bits(GetBitContext *s, const uint8_t *buffer, int bit_size)
Initialize GetBitContext.
Definition: get_bits.h:406
#define mid_pred
Definition: mathops.h:96
static int build_huff10(const uint8_t *src, VLC *vlc, int *fsym)
Definition: utvideodec.c:38
#define u(width,...)
uint8_t * slice_bits
Definition: utvideo.h:82
#define AV_PIX_FMT_YUV422P10
Definition: pixfmt.h:342
int ff_ut_huff_cmp_len(const void *a, const void *b)
Definition: utvideo.c:37
uint8_t * data[AV_NUM_DATA_POINTERS]
pointer to the picture/channel planes.
Definition: frame.h:198
static int decode(AVCodecContext *avctx, void *data, int *got_sub, AVPacket *avpkt)
Definition: ccaption_dec.c:722
GLint GLenum GLboolean GLsizei stride
Definition: opengl_enc.c:105
void av_fast_malloc(void *ptr, unsigned int *size, size_t min_size)
Allocate a buffer, reusing the given one if large enough.
Definition: mem.c:499
planar YUV 4:2:0, 12bpp, (1 Cr & Cb sample per 2x2 Y samples)
Definition: pixfmt.h:62
static double c[64]
#define AV_INPUT_BUFFER_PADDING_SIZE
Required number of additionally allocated bytes at the end of the input bitstream for decoding...
Definition: avcodec.h:731
static int slice_end(AVCodecContext *avctx, AVFrame *pict)
Handle slice ends.
Definition: mpeg12dec.c:2051
av_cold void ff_bswapdsp_init(BswapDSPContext *c)
Definition: bswapdsp.c:49
void * priv_data
Definition: avcodec.h:1691
int len
VLC_TYPE(* table)[2]
code, bits
Definition: vlc.h:28
int key_frame
1 -> keyframe, 0-> not
Definition: frame.h:253
#define av_freep(p)
#define stride
#define MKTAG(a, b, c, d)
Definition: common.h:342
uint64_t_TMPL AV_WL64 unsigned int_TMPL AV_RL32
Definition: bytestream.h:87
AVCodec ff_utvideo_decoder
Definition: utvideodec.c:800
This structure stores compressed data.
Definition: avcodec.h:1557
void ff_free_vlc(VLC *vlc)
Definition: bitstream.c:360
#define AV_CODEC_CAP_DR1
Codec uses get_buffer() for allocating buffers and supports custom allocators.
Definition: avcodec.h:956
for(j=16;j >0;--j)