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 #undef A
376 #undef B
377 #undef C
378 
380  int width, int height, int slices, int rmode)
381 {
382  int i, j, slice;
383  int A, B, C;
384  uint8_t *bsrc;
385  int slice_start, slice_height;
386  const int cmask = ~rmode;
387 
388  for (slice = 0; slice < slices; slice++) {
389  slice_start = ((slice * height) / slices) & cmask;
390  slice_height = ((((slice + 1) * height) / slices) & cmask) -
391  slice_start;
392 
393  if (!slice_height)
394  continue;
395  bsrc = src + slice_start * stride;
396 
397  // first line - left neighbour prediction
398  bsrc[0] += 0x80;
399  c->llviddsp.add_left_pred(bsrc, bsrc, width, 0);
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 = 1; i < width; i++) {
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  c->llviddsp.add_median_pred(bsrc, bsrc - stride,
417  bsrc, width, &A, &B);
418  bsrc += stride;
419  }
420  }
421 }
422 
423 /* UtVideo interlaced mode treats every two lines as a single one,
424  * so restoring function should take care of possible padding between
425  * two parts of the same "line".
426  */
428  int width, int height, int slices, int rmode)
429 {
430  int i, j, slice;
431  int A, B, C;
432  uint8_t *bsrc;
433  int slice_start, slice_height;
434  const int cmask = ~(rmode ? 3 : 1);
435  const int stride2 = stride << 1;
436 
437  for (slice = 0; slice < slices; slice++) {
438  slice_start = ((slice * height) / slices) & cmask;
439  slice_height = ((((slice + 1) * height) / slices) & cmask) -
440  slice_start;
441  slice_height >>= 1;
442  if (!slice_height)
443  continue;
444 
445  bsrc = src + slice_start * stride;
446 
447  // first line - left neighbour prediction
448  bsrc[0] += 0x80;
449  A = c->llviddsp.add_left_pred(bsrc, bsrc, width, 0);
450  c->llviddsp.add_left_pred(bsrc + stride, bsrc + stride, width, A);
451  bsrc += stride2;
452  if (slice_height <= 1)
453  continue;
454  // second line - first element has top prediction, the rest uses median
455  C = bsrc[-stride2];
456  bsrc[0] += C;
457  A = bsrc[0];
458  for (i = 1; i < width; i++) {
459  B = bsrc[i - stride2];
460  bsrc[i] += mid_pred(A, B, (uint8_t)(A + B - C));
461  C = B;
462  A = bsrc[i];
463  }
464  c->llviddsp.add_median_pred(bsrc + stride, bsrc - stride,
465  bsrc + stride, width, &A, &B);
466  bsrc += stride2;
467  // the rest of lines use continuous median prediction
468  for (j = 2; j < slice_height; j++) {
469  c->llviddsp.add_median_pred(bsrc, bsrc - stride2,
470  bsrc, width, &A, &B);
471  c->llviddsp.add_median_pred(bsrc + stride, bsrc - stride,
472  bsrc + stride, width, &A, &B);
473  bsrc += stride2;
474  }
475  }
476 }
477 
478 static void restore_median_packed(uint8_t *src, int step, int stride,
479  int width, int height, int slices, int rmode)
480 {
481  int i, j, slice;
482  int A, B, C;
483  uint8_t *bsrc;
484  int slice_start, slice_height;
485  const int cmask = ~rmode;
486 
487  for (slice = 0; slice < slices; slice++) {
488  slice_start = ((slice * height) / slices) & cmask;
489  slice_height = ((((slice + 1) * height) / slices) & cmask) -
490  slice_start;
491 
492  if (!slice_height)
493  continue;
494  bsrc = src + slice_start * stride;
495 
496  // first line - left neighbour prediction
497  bsrc[0] += 0x80;
498  A = bsrc[0];
499  for (i = step; i < width * step; i += step) {
500  bsrc[i] += A;
501  A = bsrc[i];
502  }
503  bsrc += stride;
504  if (slice_height <= 1)
505  continue;
506  // second line - first element has top prediction, the rest uses median
507  C = bsrc[-stride];
508  bsrc[0] += C;
509  A = bsrc[0];
510  for (i = step; i < width * step; i += step) {
511  B = bsrc[i - stride];
512  bsrc[i] += mid_pred(A, B, (uint8_t)(A + B - C));
513  C = B;
514  A = bsrc[i];
515  }
516  bsrc += stride;
517  // the rest of lines use continuous median prediction
518  for (j = 2; j < slice_height; j++) {
519  for (i = 0; i < width * step; i += step) {
520  B = bsrc[i - stride];
521  bsrc[i] += mid_pred(A, B, (uint8_t)(A + B - C));
522  C = B;
523  A = bsrc[i];
524  }
525  bsrc += stride;
526  }
527  }
528 }
529 
530 /* UtVideo interlaced mode treats every two lines as a single one,
531  * so restoring function should take care of possible padding between
532  * two parts of the same "line".
533  */
534 static void restore_median_packed_il(uint8_t *src, int step, int stride,
535  int width, int height, int slices, int rmode)
536 {
537  int i, j, slice;
538  int A, B, C;
539  uint8_t *bsrc;
540  int slice_start, slice_height;
541  const int cmask = ~(rmode ? 3 : 1);
542  const int stride2 = stride << 1;
543 
544  for (slice = 0; slice < slices; slice++) {
545  slice_start = ((slice * height) / slices) & cmask;
546  slice_height = ((((slice + 1) * height) / slices) & cmask) -
547  slice_start;
548  slice_height >>= 1;
549  if (!slice_height)
550  continue;
551 
552  bsrc = src + slice_start * stride;
553 
554  // first line - left neighbour prediction
555  bsrc[0] += 0x80;
556  A = bsrc[0];
557  for (i = step; i < width * step; i += step) {
558  bsrc[i] += A;
559  A = bsrc[i];
560  }
561  for (i = 0; i < width * step; i += step) {
562  bsrc[stride + i] += A;
563  A = bsrc[stride + i];
564  }
565  bsrc += stride2;
566  if (slice_height <= 1)
567  continue;
568  // second line - first element has top prediction, the rest uses median
569  C = bsrc[-stride2];
570  bsrc[0] += C;
571  A = bsrc[0];
572  for (i = step; i < width * step; i += step) {
573  B = bsrc[i - stride2];
574  bsrc[i] += mid_pred(A, B, (uint8_t)(A + B - C));
575  C = B;
576  A = bsrc[i];
577  }
578  for (i = 0; i < width * step; i += step) {
579  B = bsrc[i - stride];
580  bsrc[stride + i] += mid_pred(A, B, (uint8_t)(A + B - C));
581  C = B;
582  A = bsrc[stride + i];
583  }
584  bsrc += stride2;
585  // the rest of lines use continuous median prediction
586  for (j = 2; j < slice_height; j++) {
587  for (i = 0; i < width * step; i += step) {
588  B = bsrc[i - stride2];
589  bsrc[i] += mid_pred(A, B, (uint8_t)(A + B - C));
590  C = B;
591  A = bsrc[i];
592  }
593  for (i = 0; i < width * step; i += step) {
594  B = bsrc[i - stride];
595  bsrc[i + stride] += mid_pred(A, B, (uint8_t)(A + B - C));
596  C = B;
597  A = bsrc[i + stride];
598  }
599  bsrc += stride2;
600  }
601  }
602 }
603 
604 static int decode_frame(AVCodecContext *avctx, void *data, int *got_frame,
605  AVPacket *avpkt)
606 {
607  const uint8_t *buf = avpkt->data;
608  int buf_size = avpkt->size;
609  UtvideoContext *c = avctx->priv_data;
610  int i, j;
611  const uint8_t *plane_start[5];
612  int plane_size, max_slice_size = 0, slice_start, slice_end, slice_size;
613  int ret;
614  GetByteContext gb;
615  ThreadFrame frame = { .f = data };
616 
617  if ((ret = ff_thread_get_buffer(avctx, &frame, 0)) < 0)
618  return ret;
619 
620  /* parse plane structure to get frame flags and validate slice offsets */
621  bytestream2_init(&gb, buf, buf_size);
622  if (c->pro) {
624  av_log(avctx, AV_LOG_ERROR, "Not enough data for frame information\n");
625  return AVERROR_INVALIDDATA;
626  }
627  c->frame_info = bytestream2_get_le32u(&gb);
628  c->slices = ((c->frame_info >> 16) & 0xff) + 1;
629  for (i = 0; i < c->planes; i++) {
630  plane_start[i] = gb.buffer;
631  if (bytestream2_get_bytes_left(&gb) < 1024 + 4 * c->slices) {
632  av_log(avctx, AV_LOG_ERROR, "Insufficient data for a plane\n");
633  return AVERROR_INVALIDDATA;
634  }
635  slice_start = 0;
636  slice_end = 0;
637  for (j = 0; j < c->slices; j++) {
638  slice_end = bytestream2_get_le32u(&gb);
639  if (slice_end < 0 || slice_end < slice_start ||
640  bytestream2_get_bytes_left(&gb) < slice_end) {
641  av_log(avctx, AV_LOG_ERROR, "Incorrect slice size\n");
642  return AVERROR_INVALIDDATA;
643  }
644  slice_size = slice_end - slice_start;
645  slice_start = slice_end;
646  max_slice_size = FFMAX(max_slice_size, slice_size);
647  }
648  plane_size = slice_end;
649  bytestream2_skipu(&gb, plane_size);
650  bytestream2_skipu(&gb, 1024);
651  }
652  plane_start[c->planes] = gb.buffer;
653  } else {
654  for (i = 0; i < c->planes; i++) {
655  plane_start[i] = gb.buffer;
656  if (bytestream2_get_bytes_left(&gb) < 256 + 4 * c->slices) {
657  av_log(avctx, AV_LOG_ERROR, "Insufficient data for a plane\n");
658  return AVERROR_INVALIDDATA;
659  }
660  bytestream2_skipu(&gb, 256);
661  slice_start = 0;
662  slice_end = 0;
663  for (j = 0; j < c->slices; j++) {
664  slice_end = bytestream2_get_le32u(&gb);
665  if (slice_end < 0 || slice_end < slice_start ||
666  bytestream2_get_bytes_left(&gb) < slice_end) {
667  av_log(avctx, AV_LOG_ERROR, "Incorrect slice size\n");
668  return AVERROR_INVALIDDATA;
669  }
670  slice_size = slice_end - slice_start;
671  slice_start = slice_end;
672  max_slice_size = FFMAX(max_slice_size, slice_size);
673  }
674  plane_size = slice_end;
675  bytestream2_skipu(&gb, plane_size);
676  }
677  plane_start[c->planes] = gb.buffer;
679  av_log(avctx, AV_LOG_ERROR, "Not enough data for frame information\n");
680  return AVERROR_INVALIDDATA;
681  }
682  c->frame_info = bytestream2_get_le32u(&gb);
683  }
684  av_log(avctx, AV_LOG_DEBUG, "frame information flags %"PRIX32"\n",
685  c->frame_info);
686 
687  c->frame_pred = (c->frame_info >> 8) & 3;
688 
689  if (c->frame_pred == PRED_GRADIENT) {
690  avpriv_request_sample(avctx, "Frame with gradient prediction");
691  return AVERROR_PATCHWELCOME;
692  }
693 
695  max_slice_size + AV_INPUT_BUFFER_PADDING_SIZE);
696 
697  if (!c->slice_bits) {
698  av_log(avctx, AV_LOG_ERROR, "Cannot allocate temporary buffer\n");
699  return AVERROR(ENOMEM);
700  }
701 
702  switch (c->avctx->pix_fmt) {
703  case AV_PIX_FMT_RGB24:
704  case AV_PIX_FMT_RGBA:
705  for (i = 0; i < c->planes; i++) {
706  ret = decode_plane(c, i, frame.f->data[0] + ff_ut_rgb_order[i],
707  c->planes, frame.f->linesize[0], avctx->width,
708  avctx->height, plane_start[i],
709  c->frame_pred == PRED_LEFT);
710  if (ret)
711  return ret;
712  if (c->frame_pred == PRED_MEDIAN) {
713  if (!c->interlaced) {
715  c->planes, frame.f->linesize[0], avctx->width,
716  avctx->height, c->slices, 0);
717  } else {
719  c->planes, frame.f->linesize[0],
720  avctx->width, avctx->height, c->slices,
721  0);
722  }
723  }
724  }
725  restore_rgb_planes(frame.f->data[0], c->planes, frame.f->linesize[0],
726  avctx->width, avctx->height);
727  break;
728  case AV_PIX_FMT_GBRAP10:
729  case AV_PIX_FMT_GBRP10:
730  for (i = 0; i < c->planes; i++) {
731  ret = decode_plane10(c, i, (uint16_t *)frame.f->data[i], 1,
732  frame.f->linesize[i] / 2, avctx->width,
733  avctx->height, plane_start[i],
734  plane_start[i + 1] - 1024,
735  c->frame_pred == PRED_LEFT);
736  if (ret)
737  return ret;
738  }
739  restore_rgb_planes10(frame.f, avctx->width, avctx->height);
740  break;
741  case AV_PIX_FMT_YUV420P:
742  for (i = 0; i < 3; i++) {
743  ret = decode_plane(c, i, frame.f->data[i], 1, frame.f->linesize[i],
744  avctx->width >> !!i, avctx->height >> !!i,
745  plane_start[i], c->frame_pred == PRED_LEFT);
746  if (ret)
747  return ret;
748  if (c->frame_pred == PRED_MEDIAN) {
749  if (!c->interlaced) {
750  restore_median_planar(c, frame.f->data[i], frame.f->linesize[i],
751  avctx->width >> !!i, avctx->height >> !!i,
752  c->slices, !i);
753  } else {
754  restore_median_planar_il(c, frame.f->data[i], frame.f->linesize[i],
755  avctx->width >> !!i,
756  avctx->height >> !!i,
757  c->slices, !i);
758  }
759  }
760  }
761  break;
762  case AV_PIX_FMT_YUV422P:
763  for (i = 0; i < 3; i++) {
764  ret = decode_plane(c, i, frame.f->data[i], 1, frame.f->linesize[i],
765  avctx->width >> !!i, avctx->height,
766  plane_start[i], c->frame_pred == PRED_LEFT);
767  if (ret)
768  return ret;
769  if (c->frame_pred == PRED_MEDIAN) {
770  if (!c->interlaced) {
771  restore_median_planar(c, frame.f->data[i], frame.f->linesize[i],
772  avctx->width >> !!i, avctx->height,
773  c->slices, 0);
774  } else {
775  restore_median_planar_il(c, frame.f->data[i], frame.f->linesize[i],
776  avctx->width >> !!i, avctx->height,
777  c->slices, 0);
778  }
779  }
780  }
781  break;
782  case AV_PIX_FMT_YUV444P:
783  for (i = 0; i < 3; i++) {
784  ret = decode_plane(c, i, frame.f->data[i], 1, frame.f->linesize[i],
785  avctx->width, avctx->height,
786  plane_start[i], c->frame_pred == PRED_LEFT);
787  if (ret)
788  return ret;
789  if (c->frame_pred == PRED_MEDIAN) {
790  if (!c->interlaced) {
791  restore_median_planar(c, frame.f->data[i], frame.f->linesize[i],
792  avctx->width, avctx->height,
793  c->slices, 0);
794  } else {
795  restore_median_planar_il(c, frame.f->data[i], frame.f->linesize[i],
796  avctx->width, avctx->height,
797  c->slices, 0);
798  }
799  }
800  }
801  break;
803  for (i = 0; i < 3; i++) {
804  ret = decode_plane10(c, i, (uint16_t *)frame.f->data[i], 1, frame.f->linesize[i] / 2,
805  avctx->width >> !!i, avctx->height,
806  plane_start[i], plane_start[i + 1] - 1024, c->frame_pred == PRED_LEFT);
807  if (ret)
808  return ret;
809  }
810  break;
811  }
812 
813  frame.f->key_frame = 1;
814  frame.f->pict_type = AV_PICTURE_TYPE_I;
815  frame.f->interlaced_frame = !!c->interlaced;
816 
817  *got_frame = 1;
818 
819  /* always report that the buffer was completely consumed */
820  return buf_size;
821 }
822 
824 {
825  UtvideoContext * const c = avctx->priv_data;
826 
827  c->avctx = avctx;
828 
829  ff_bswapdsp_init(&c->bdsp);
831 
832  if (avctx->extradata_size >= 16) {
833  av_log(avctx, AV_LOG_DEBUG, "Encoder version %d.%d.%d.%d\n",
834  avctx->extradata[3], avctx->extradata[2],
835  avctx->extradata[1], avctx->extradata[0]);
836  av_log(avctx, AV_LOG_DEBUG, "Original format %"PRIX32"\n",
837  AV_RB32(avctx->extradata + 4));
838  c->frame_info_size = AV_RL32(avctx->extradata + 8);
839  c->flags = AV_RL32(avctx->extradata + 12);
840 
841  if (c->frame_info_size != 4)
842  avpriv_request_sample(avctx, "Frame info not 4 bytes");
843  av_log(avctx, AV_LOG_DEBUG, "Encoding parameters %08"PRIX32"\n", c->flags);
844  c->slices = (c->flags >> 24) + 1;
845  c->compression = c->flags & 1;
846  c->interlaced = c->flags & 0x800;
847  } else if (avctx->extradata_size == 8) {
848  av_log(avctx, AV_LOG_DEBUG, "Encoder version %d.%d.%d.%d\n",
849  avctx->extradata[3], avctx->extradata[2],
850  avctx->extradata[1], avctx->extradata[0]);
851  av_log(avctx, AV_LOG_DEBUG, "Original format %"PRIX32"\n",
852  AV_RB32(avctx->extradata + 4));
853  c->interlaced = 0;
854  c->pro = 1;
855  c->frame_info_size = 4;
856  } else {
857  av_log(avctx, AV_LOG_ERROR,
858  "Insufficient extradata size %d, should be at least 16\n",
859  avctx->extradata_size);
860  return AVERROR_INVALIDDATA;
861  }
862 
863  c->slice_bits_size = 0;
864 
865  switch (avctx->codec_tag) {
866  case MKTAG('U', 'L', 'R', 'G'):
867  c->planes = 3;
868  avctx->pix_fmt = AV_PIX_FMT_RGB24;
869  break;
870  case MKTAG('U', 'L', 'R', 'A'):
871  c->planes = 4;
872  avctx->pix_fmt = AV_PIX_FMT_RGBA;
873  break;
874  case MKTAG('U', 'L', 'Y', '0'):
875  c->planes = 3;
876  avctx->pix_fmt = AV_PIX_FMT_YUV420P;
877  avctx->colorspace = AVCOL_SPC_BT470BG;
878  break;
879  case MKTAG('U', 'L', 'Y', '2'):
880  c->planes = 3;
881  avctx->pix_fmt = AV_PIX_FMT_YUV422P;
882  avctx->colorspace = AVCOL_SPC_BT470BG;
883  break;
884  case MKTAG('U', 'L', 'Y', '4'):
885  c->planes = 3;
886  avctx->pix_fmt = AV_PIX_FMT_YUV444P;
887  avctx->colorspace = AVCOL_SPC_BT470BG;
888  break;
889  case MKTAG('U', 'Q', 'Y', '2'):
890  c->planes = 3;
891  avctx->pix_fmt = AV_PIX_FMT_YUV422P10;
892  break;
893  case MKTAG('U', 'Q', 'R', 'G'):
894  c->planes = 3;
895  avctx->pix_fmt = AV_PIX_FMT_GBRP10;
896  break;
897  case MKTAG('U', 'Q', 'R', 'A'):
898  c->planes = 4;
899  avctx->pix_fmt = AV_PIX_FMT_GBRAP10;
900  break;
901  case MKTAG('U', 'L', 'H', '0'):
902  c->planes = 3;
903  avctx->pix_fmt = AV_PIX_FMT_YUV420P;
904  avctx->colorspace = AVCOL_SPC_BT709;
905  break;
906  case MKTAG('U', 'L', 'H', '2'):
907  c->planes = 3;
908  avctx->pix_fmt = AV_PIX_FMT_YUV422P;
909  avctx->colorspace = AVCOL_SPC_BT709;
910  break;
911  case MKTAG('U', 'L', 'H', '4'):
912  c->planes = 3;
913  avctx->pix_fmt = AV_PIX_FMT_YUV444P;
914  avctx->colorspace = AVCOL_SPC_BT709;
915  break;
916  default:
917  av_log(avctx, AV_LOG_ERROR, "Unknown Ut Video FOURCC provided (%08X)\n",
918  avctx->codec_tag);
919  return AVERROR_INVALIDDATA;
920  }
921 
922  return 0;
923 }
924 
926 {
927  UtvideoContext * const c = avctx->priv_data;
928 
929  av_freep(&c->slice_bits);
930 
931  return 0;
932 }
933 
935  .name = "utvideo",
936  .long_name = NULL_IF_CONFIG_SMALL("Ut Video"),
937  .type = AVMEDIA_TYPE_VIDEO,
938  .id = AV_CODEC_ID_UTVIDEO,
939  .priv_data_size = sizeof(UtvideoContext),
940  .init = decode_init,
941  .close = decode_end,
942  .decode = decode_frame,
944 };
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:452
#define AVERROR_INVALIDDATA
Invalid data found when processing input.
Definition: error.h:59
static void restore_median_planar(UtvideoContext *c, uint8_t *src, int stride, int width, int height, int slices, int rmode)
Definition: utvideodec.c:379
static void restore_median_planar_il(UtvideoContext *c, uint8_t *src, int stride, int width, int height, int slices, int rmode)
Definition: utvideodec.c:427
This structure describes decoded (raw) audio or video data.
Definition: frame.h:190
int ff_ut10_huff_cmp_len(const void *a, const void *b)
Definition: utvideo.c:43
void(* add_median_pred)(uint8_t *dst, const uint8_t *top, const uint8_t *diff, intptr_t w, int *left, int *left_top)
ptrdiff_t const GLvoid * data
Definition: opengl_enc.c:101
#define C
#define AV_PIX_FMT_GBRAP10
Definition: pixfmt.h:371
uint32_t flags
Definition: utvideo.h:76
planar YUV 4:4:4, 24bpp, (1 Cr & Cb sample per 1x1 Y samples)
Definition: pixfmt.h:67
#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:112
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:456
int(* add_left_pred)(uint8_t *dst, const uint8_t *src, intptr_t w, int left)
int slice_bits_size
Definition: utvideo.h:86
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:269
int size
Definition: avcodec.h:1613
const char * b
Definition: vf_curves.c:113
#define AV_PIX_FMT_GBRP10
Definition: pixfmt.h:367
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:925
enum AVPixelFormat pix_fmt
Pixel format, see AV_PIX_FMT_xxx.
Definition: avcodec.h:1915
static av_always_inline void bytestream2_init(GetByteContext *g, const uint8_t *buf, int buf_size)
Definition: bytestream.h:133
#define src
Definition: vp8dsp.c:254
AVCodec.
Definition: avcodec.h:3620
int interlaced
Definition: utvideo.h:80
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.
uint8_t * extradata
some codecs need / can use extradata like Huffman tables.
Definition: avcodec.h:1802
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:76
static AVFrame * frame
#define height
uint8_t * data
Definition: avcodec.h:1612
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:328
#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:72
static int get_bits_left(GetBitContext *gb)
Definition: get_bits.h:587
#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:823
#define AVERROR(e)
Definition: error.h:43
#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:71
const char * r
Definition: vf_curves.c:111
#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:3627
uint32_t frame_info_size
Definition: utvideo.h:76
#define FFMAX(a, b)
Definition: common.h:94
#define fail()
Definition: checkasm.h:84
#define AV_CODEC_CAP_FRAME_THREADS
Codec supports frame-level multithreading.
Definition: avcodec.h:1027
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
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
int compression
Definition: utvideo.h:79
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:264
#define FFMIN(a, b)
Definition: common.h:96
#define width
int width
picture width / height.
Definition: avcodec.h:1874
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:554
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:604
Common Ut Video header.
int frame_pred
Definition: utvideo.h:81
uint8_t len
Definition: magicyuv.c:49
Libavcodec external API header.
int linesize[AV_NUM_DATA_POINTERS]
For video, size in bytes of each picture line.
Definition: frame.h:221
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:1687
unsigned int codec_tag
fourcc (LSB first, so "ABCD" -> ('D'<<24) + ('C'<<16) + ('B'<<8) + 'A').
Definition: avcodec.h:1719
void * buf
Definition: avisynth_c.h:690
int extradata_size
Definition: avcodec.h:1803
void ff_llviddsp_init(LLVidDSPContext *c)
enum AVColorSpace colorspace
YUV colorspace type.
Definition: avcodec.h:2417
static int init_get_bits(GetBitContext *s, const uint8_t *buffer, int bit_size)
Initialize GetBitContext.
Definition: get_bits.h:425
#define mid_pred
Definition: mathops.h:97
static int build_huff10(const uint8_t *src, VLC *vlc, int *fsym)
Definition: utvideodec.c:38
#define u(width,...)
static void restore_median_packed(uint8_t *src, int step, int stride, int width, int height, int slices, int rmode)
Definition: utvideodec.c:478
uint8_t * slice_bits
Definition: utvideo.h:85
#define AV_PIX_FMT_YUV422P10
Definition: pixfmt.h:352
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:204
LLVidDSPContext llviddsp
Definition: utvideo.h:73
static void restore_median_packed_il(uint8_t *src, int step, int stride, int width, int height, int slices, int rmode)
Definition: utvideodec.c:534
GLint GLenum GLboolean GLsizei stride
Definition: opengl_enc.c:105
planar YUV 4:2:0, 12bpp, (1 Cr & Cb sample per 2x2 Y samples)
Definition: pixfmt.h:62
uint16_t sym
Definition: magicyuv.c:48
#define AV_INPUT_BUFFER_PADDING_SIZE
Required number of additionally allocated bytes at the end of the input bitstream for decoding...
Definition: avcodec.h:739
static int slice_end(AVCodecContext *avctx, AVFrame *pict)
Handle slice ends.
Definition: mpeg12dec.c:2050
av_cold void ff_bswapdsp_init(BswapDSPContext *c)
Definition: bswapdsp.c:49
void * priv_data
Definition: avcodec.h:1729
Definition: vf_geq.c:46
int len
VLC_TYPE(* table)[2]
code, bits
Definition: vlc.h:28
int key_frame
1 -> keyframe, 0-> not
Definition: frame.h:259
static AVCodec * c
#define av_freep(p)
static int decode(AVCodecContext *avctx, AVFrame *frame, int *got_frame, AVPacket *pkt)
Definition: ffmpeg.c:2040
#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:934
This structure stores compressed data.
Definition: avcodec.h:1589
void ff_free_vlc(VLC *vlc)
Definition: bitstream.c:354
#define AV_CODEC_CAP_DR1
Codec uses get_buffer() for allocating buffers and supports custom allocators.
Definition: avcodec.h:964
for(j=16;j >0;--j)