FFmpeg
huffyuvenc.c
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2002-2014 Michael Niedermayer <michaelni@gmx.at>
3  *
4  * see http://www.pcisys.net/~melanson/codecs/huffyuv.txt for a description of
5  * the algorithm used
6  *
7  * This file is part of FFmpeg.
8  *
9  * FFmpeg is free software; you can redistribute it and/or
10  * modify it under the terms of the GNU Lesser General Public
11  * License as published by the Free Software Foundation; either
12  * version 2.1 of the License, or (at your option) any later version.
13  *
14  * FFmpeg is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17  * Lesser General Public License for more details.
18  *
19  * You should have received a copy of the GNU Lesser General Public
20  * License along with FFmpeg; if not, write to the Free Software
21  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
22  *
23  * yuva, gray, 4:4:4, 4:1:1, 4:1:0 and >8 bit per sample support sponsored by NOA
24  */
25 
26 /**
27  * @file
28  * huffyuv encoder
29  */
30 
31 #include "avcodec.h"
32 #include "huffyuv.h"
33 #include "huffman.h"
34 #include "huffyuvencdsp.h"
35 #include "internal.h"
36 #include "lossless_videoencdsp.h"
37 #include "put_bits.h"
38 #include "libavutil/opt.h"
39 #include "libavutil/pixdesc.h"
40 
41 static inline void diff_bytes(HYuvContext *s, uint8_t *dst,
42  const uint8_t *src0, const uint8_t *src1, int w)
43 {
44  if (s->bps <= 8) {
45  s->llvidencdsp.diff_bytes(dst, src0, src1, w);
46  } else {
47  s->hencdsp.diff_int16((uint16_t *)dst, (const uint16_t *)src0, (const uint16_t *)src1, s->n - 1, w);
48  }
49 }
50 
51 static inline int sub_left_prediction(HYuvContext *s, uint8_t *dst,
52  const uint8_t *src, int w, int left)
53 {
54  int i;
55  int min_width = FFMIN(w, 32);
56 
57  if (s->bps <= 8) {
58  for (i = 0; i < min_width; i++) { /* scalar loop before dsp call */
59  const int temp = src[i];
60  dst[i] = temp - left;
61  left = temp;
62  }
63  if (w < 32)
64  return left;
65  s->llvidencdsp.diff_bytes(dst + 32, src + 32, src + 31, w - 32);
66  return src[w-1];
67  } else {
68  const uint16_t *src16 = (const uint16_t *)src;
69  uint16_t *dst16 = ( uint16_t *)dst;
70  for (i = 0; i < min_width; i++) { /* scalar loop before dsp call */
71  const int temp = src16[i];
72  dst16[i] = temp - left;
73  left = temp;
74  }
75  if (w < 32)
76  return left;
77  s->hencdsp.diff_int16(dst16 + 32, src16 + 32, src16 + 31, s->n - 1, w - 32);
78  return src16[w-1];
79  }
80 }
81 
82 static inline void sub_left_prediction_bgr32(HYuvContext *s, uint8_t *dst,
83  const uint8_t *src, int w,
84  int *red, int *green, int *blue,
85  int *alpha)
86 {
87  int i;
88  int r, g, b, a;
89  int min_width = FFMIN(w, 8);
90  r = *red;
91  g = *green;
92  b = *blue;
93  a = *alpha;
94 
95  for (i = 0; i < min_width; i++) {
96  const int rt = src[i * 4 + R];
97  const int gt = src[i * 4 + G];
98  const int bt = src[i * 4 + B];
99  const int at = src[i * 4 + A];
100  dst[i * 4 + R] = rt - r;
101  dst[i * 4 + G] = gt - g;
102  dst[i * 4 + B] = bt - b;
103  dst[i * 4 + A] = at - a;
104  r = rt;
105  g = gt;
106  b = bt;
107  a = at;
108  }
109 
110  s->llvidencdsp.diff_bytes(dst + 32, src + 32, src + 32 - 4, w * 4 - 32);
111 
112  *red = src[(w - 1) * 4 + R];
113  *green = src[(w - 1) * 4 + G];
114  *blue = src[(w - 1) * 4 + B];
115  *alpha = src[(w - 1) * 4 + A];
116 }
117 
118 static inline void sub_left_prediction_rgb24(HYuvContext *s, uint8_t *dst,
119  uint8_t *src, int w,
120  int *red, int *green, int *blue)
121 {
122  int i;
123  int r, g, b;
124  r = *red;
125  g = *green;
126  b = *blue;
127  for (i = 0; i < FFMIN(w, 16); i++) {
128  const int rt = src[i * 3 + 0];
129  const int gt = src[i * 3 + 1];
130  const int bt = src[i * 3 + 2];
131  dst[i * 3 + 0] = rt - r;
132  dst[i * 3 + 1] = gt - g;
133  dst[i * 3 + 2] = bt - b;
134  r = rt;
135  g = gt;
136  b = bt;
137  }
138 
139  s->llvidencdsp.diff_bytes(dst + 48, src + 48, src + 48 - 3, w * 3 - 48);
140 
141  *red = src[(w - 1) * 3 + 0];
142  *green = src[(w - 1) * 3 + 1];
143  *blue = src[(w - 1) * 3 + 2];
144 }
145 
146 static void sub_median_prediction(HYuvContext *s, uint8_t *dst, const uint8_t *src1, const uint8_t *src2, int w, int *left, int *left_top)
147 {
148  if (s->bps <= 8) {
149  s->llvidencdsp.sub_median_pred(dst, src1, src2, w , left, left_top);
150  } else {
151  s->hencdsp.sub_hfyu_median_pred_int16((uint16_t *)dst, (const uint16_t *)src1, (const uint16_t *)src2, s->n - 1, w , left, left_top);
152  }
153 }
154 
155 static int store_table(HYuvContext *s, const uint8_t *len, uint8_t *buf)
156 {
157  int i;
158  int index = 0;
159  int n = s->vlc_n;
160 
161  for (i = 0; i < n;) {
162  int val = len[i];
163  int repeat = 0;
164 
165  for (; i < n && len[i] == val && repeat < 255; i++)
166  repeat++;
167 
168  av_assert0(val < 32 && val >0 && repeat < 256 && repeat>0);
169  if (repeat > 7) {
170  buf[index++] = val;
171  buf[index++] = repeat;
172  } else {
173  buf[index++] = val | (repeat << 5);
174  }
175  }
176 
177  return index;
178 }
179 
180 static int store_huffman_tables(HYuvContext *s, uint8_t *buf)
181 {
182  int i, ret;
183  int size = 0;
184  int count = 3;
185 
186  if (s->version > 2)
187  count = 1 + s->alpha + 2*s->chroma;
188 
189  for (i = 0; i < count; i++) {
190  if ((ret = ff_huff_gen_len_table(s->len[i], s->stats[i], s->vlc_n, 0)) < 0)
191  return ret;
192 
193  if (ff_huffyuv_generate_bits_table(s->bits[i], s->len[i], s->vlc_n) < 0) {
194  return -1;
195  }
196 
197  size += store_table(s, s->len[i], buf + size);
198  }
199  return size;
200 }
201 
203 {
204  HYuvContext *s = avctx->priv_data;
205  int i, j;
206  int ret;
208 
209  ff_huffyuv_common_init(avctx);
210  ff_huffyuvencdsp_init(&s->hencdsp, avctx);
211  ff_llvidencdsp_init(&s->llvidencdsp);
212 
213  avctx->extradata = av_mallocz(3*MAX_N + 4);
214  if (s->flags&AV_CODEC_FLAG_PASS1) {
215 #define STATS_OUT_SIZE 21*MAX_N*3 + 4
216  avctx->stats_out = av_mallocz(STATS_OUT_SIZE); // 21*256*3(%llu ) + 3(\n) + 1(0) = 16132
217  if (!avctx->stats_out)
218  return AVERROR(ENOMEM);
219  }
220  s->version = 2;
221 
222  if (!avctx->extradata)
223  return AVERROR(ENOMEM);
224 
225  s->bps = desc->comp[0].depth;
226  s->yuv = !(desc->flags & AV_PIX_FMT_FLAG_RGB) && desc->nb_components >= 2;
227  s->chroma = desc->nb_components > 2;
228  s->alpha = !!(desc->flags & AV_PIX_FMT_FLAG_ALPHA);
230  &s->chroma_h_shift,
231  &s->chroma_v_shift);
232 
233  switch (avctx->pix_fmt) {
234  case AV_PIX_FMT_YUV420P:
235  case AV_PIX_FMT_YUV422P:
236  if (s->width & 1) {
237  av_log(avctx, AV_LOG_ERROR, "Width must be even for this colorspace.\n");
238  return AVERROR(EINVAL);
239  }
240  s->bitstream_bpp = avctx->pix_fmt == AV_PIX_FMT_YUV420P ? 12 : 16;
241  break;
242  case AV_PIX_FMT_YUV444P:
243  case AV_PIX_FMT_YUV410P:
244  case AV_PIX_FMT_YUV411P:
245  case AV_PIX_FMT_YUV440P:
246  case AV_PIX_FMT_GBRP:
247  case AV_PIX_FMT_GBRP9:
248  case AV_PIX_FMT_GBRP10:
249  case AV_PIX_FMT_GBRP12:
250  case AV_PIX_FMT_GBRP14:
251  case AV_PIX_FMT_GBRP16:
252  case AV_PIX_FMT_GRAY8:
253  case AV_PIX_FMT_GRAY16:
254  case AV_PIX_FMT_YUVA444P:
255  case AV_PIX_FMT_YUVA420P:
256  case AV_PIX_FMT_YUVA422P:
257  case AV_PIX_FMT_GBRAP:
258  case AV_PIX_FMT_YUV420P9:
263  case AV_PIX_FMT_YUV422P9:
268  case AV_PIX_FMT_YUV444P9:
282  s->version = 3;
283  break;
284  case AV_PIX_FMT_RGB32:
285  s->bitstream_bpp = 32;
286  break;
287  case AV_PIX_FMT_RGB24:
288  s->bitstream_bpp = 24;
289  break;
290  default:
291  av_log(avctx, AV_LOG_ERROR, "format not supported\n");
292  return AVERROR(EINVAL);
293  }
294  s->n = 1<<s->bps;
295  s->vlc_n = FFMIN(s->n, MAX_VLC_N);
296 
297  avctx->bits_per_coded_sample = s->bitstream_bpp;
298  s->decorrelate = s->bitstream_bpp >= 24 && !s->yuv && !(desc->flags & AV_PIX_FMT_FLAG_PLANAR);
299  s->interlaced = avctx->flags & AV_CODEC_FLAG_INTERLACED_ME ? 1 : 0;
300  if (s->context) {
301  if (s->flags & (AV_CODEC_FLAG_PASS1 | AV_CODEC_FLAG_PASS2)) {
302  av_log(avctx, AV_LOG_ERROR,
303  "context=1 is not compatible with "
304  "2 pass huffyuv encoding\n");
305  return AVERROR(EINVAL);
306  }
307  }
308 
309  if (avctx->codec->id == AV_CODEC_ID_HUFFYUV) {
310  if (avctx->pix_fmt == AV_PIX_FMT_YUV420P) {
311  av_log(avctx, AV_LOG_ERROR,
312  "Error: YV12 is not supported by huffyuv; use "
313  "vcodec=ffvhuff or format=422p\n");
314  return AVERROR(EINVAL);
315  }
316  if (s->interlaced != ( s->height > 288 ))
317  av_log(avctx, AV_LOG_INFO,
318  "using huffyuv 2.2.0 or newer interlacing flag\n");
319  }
320 
321  if (s->version > 3 && avctx->strict_std_compliance > FF_COMPLIANCE_EXPERIMENTAL) {
322  av_log(avctx, AV_LOG_ERROR, "Ver > 3 is under development, files encoded with it may not be decodable with future versions!!!\n"
323  "Use vstrict=-2 / -strict -2 to use it anyway.\n");
324  return AVERROR(EINVAL);
325  }
326 
327  if (s->bitstream_bpp >= 24 && s->predictor == MEDIAN && s->version <= 2) {
328  av_log(avctx, AV_LOG_ERROR,
329  "Error: RGB is incompatible with median predictor\n");
330  return AVERROR(EINVAL);
331  }
332 
333  ((uint8_t*)avctx->extradata)[0] = s->predictor | (s->decorrelate << 6);
334  ((uint8_t*)avctx->extradata)[2] = s->interlaced ? 0x10 : 0x20;
335  if (s->context)
336  ((uint8_t*)avctx->extradata)[2] |= 0x40;
337  if (s->version < 3) {
338  ((uint8_t*)avctx->extradata)[1] = s->bitstream_bpp;
339  ((uint8_t*)avctx->extradata)[3] = 0;
340  } else {
341  ((uint8_t*)avctx->extradata)[1] = ((s->bps-1)<<4) | s->chroma_h_shift | (s->chroma_v_shift<<2);
342  if (s->chroma)
343  ((uint8_t*)avctx->extradata)[2] |= s->yuv ? 1 : 2;
344  if (s->alpha)
345  ((uint8_t*)avctx->extradata)[2] |= 4;
346  ((uint8_t*)avctx->extradata)[3] = 1;
347  }
348  s->avctx->extradata_size = 4;
349 
350  if (avctx->stats_in) {
351  char *p = avctx->stats_in;
352 
353  for (i = 0; i < 4; i++)
354  for (j = 0; j < s->vlc_n; j++)
355  s->stats[i][j] = 1;
356 
357  for (;;) {
358  for (i = 0; i < 4; i++) {
359  char *next;
360 
361  for (j = 0; j < s->vlc_n; j++) {
362  s->stats[i][j] += strtol(p, &next, 0);
363  if (next == p) return -1;
364  p = next;
365  }
366  }
367  if (p[0] == 0 || p[1] == 0 || p[2] == 0) break;
368  }
369  } else {
370  for (i = 0; i < 4; i++)
371  for (j = 0; j < s->vlc_n; j++) {
372  int d = FFMIN(j, s->vlc_n - j);
373 
374  s->stats[i][j] = 100000000 / (d*d + 1);
375  }
376  }
377 
378  ret = store_huffman_tables(s, s->avctx->extradata + s->avctx->extradata_size);
379  if (ret < 0)
380  return ret;
381  s->avctx->extradata_size += ret;
382 
383  if (s->context) {
384  for (i = 0; i < 4; i++) {
385  int pels = s->width * s->height / (i ? 40 : 10);
386  for (j = 0; j < s->vlc_n; j++) {
387  int d = FFMIN(j, s->vlc_n - j);
388  s->stats[i][j] = pels/(d*d + 1);
389  }
390  }
391  } else {
392  for (i = 0; i < 4; i++)
393  for (j = 0; j < s->vlc_n; j++)
394  s->stats[i][j]= 0;
395  }
396 
397  if (ff_huffyuv_alloc_temp(s)) {
399  return AVERROR(ENOMEM);
400  }
401 
402  s->picture_number=0;
403 
404  return 0;
405 }
406 static int encode_422_bitstream(HYuvContext *s, int offset, int count)
407 {
408  int i;
409  const uint8_t *y = s->temp[0] + offset;
410  const uint8_t *u = s->temp[1] + offset / 2;
411  const uint8_t *v = s->temp[2] + offset / 2;
412 
413  if (put_bytes_left(&s->pb, 0) < 2 * 4 * count) {
414  av_log(s->avctx, AV_LOG_ERROR, "encoded frame too large\n");
415  return -1;
416  }
417 
418 #define LOAD4\
419  int y0 = y[2 * i];\
420  int y1 = y[2 * i + 1];\
421  int u0 = u[i];\
422  int v0 = v[i];
423 
424  count /= 2;
425 
426  if (s->flags & AV_CODEC_FLAG_PASS1) {
427  for(i = 0; i < count; i++) {
428  LOAD4;
429  s->stats[0][y0]++;
430  s->stats[1][u0]++;
431  s->stats[0][y1]++;
432  s->stats[2][v0]++;
433  }
434  }
435  if (s->avctx->flags2 & AV_CODEC_FLAG2_NO_OUTPUT)
436  return 0;
437  if (s->context) {
438  for (i = 0; i < count; i++) {
439  LOAD4;
440  s->stats[0][y0]++;
441  put_bits(&s->pb, s->len[0][y0], s->bits[0][y0]);
442  s->stats[1][u0]++;
443  put_bits(&s->pb, s->len[1][u0], s->bits[1][u0]);
444  s->stats[0][y1]++;
445  put_bits(&s->pb, s->len[0][y1], s->bits[0][y1]);
446  s->stats[2][v0]++;
447  put_bits(&s->pb, s->len[2][v0], s->bits[2][v0]);
448  }
449  } else {
450  for(i = 0; i < count; i++) {
451  LOAD4;
452  put_bits(&s->pb, s->len[0][y0], s->bits[0][y0]);
453  put_bits(&s->pb, s->len[1][u0], s->bits[1][u0]);
454  put_bits(&s->pb, s->len[0][y1], s->bits[0][y1]);
455  put_bits(&s->pb, s->len[2][v0], s->bits[2][v0]);
456  }
457  }
458  return 0;
459 }
460 
461 static int encode_plane_bitstream(HYuvContext *s, int width, int plane)
462 {
463  int i, count = width/2;
464 
465  if (put_bytes_left(&s->pb, 0) < count * s->bps / 2) {
466  av_log(s->avctx, AV_LOG_ERROR, "encoded frame too large\n");
467  return -1;
468  }
469 
470 #define LOADEND\
471  int y0 = s->temp[0][width-1];
472 #define LOADEND_14\
473  int y0 = s->temp16[0][width-1] & mask;
474 #define LOADEND_16\
475  int y0 = s->temp16[0][width-1];
476 #define STATEND\
477  s->stats[plane][y0]++;
478 #define STATEND_16\
479  s->stats[plane][y0>>2]++;
480 #define WRITEEND\
481  put_bits(&s->pb, s->len[plane][y0], s->bits[plane][y0]);
482 #define WRITEEND_16\
483  put_bits(&s->pb, s->len[plane][y0>>2], s->bits[plane][y0>>2]);\
484  put_bits(&s->pb, 2, y0&3);
485 
486 #define LOAD2\
487  int y0 = s->temp[0][2 * i];\
488  int y1 = s->temp[0][2 * i + 1];
489 #define LOAD2_14\
490  int y0 = s->temp16[0][2 * i] & mask;\
491  int y1 = s->temp16[0][2 * i + 1] & mask;
492 #define LOAD2_16\
493  int y0 = s->temp16[0][2 * i];\
494  int y1 = s->temp16[0][2 * i + 1];
495 #define STAT2\
496  s->stats[plane][y0]++;\
497  s->stats[plane][y1]++;
498 #define STAT2_16\
499  s->stats[plane][y0>>2]++;\
500  s->stats[plane][y1>>2]++;
501 #define WRITE2\
502  put_bits(&s->pb, s->len[plane][y0], s->bits[plane][y0]);\
503  put_bits(&s->pb, s->len[plane][y1], s->bits[plane][y1]);
504 #define WRITE2_16\
505  put_bits(&s->pb, s->len[plane][y0>>2], s->bits[plane][y0>>2]);\
506  put_bits(&s->pb, 2, y0&3);\
507  put_bits(&s->pb, s->len[plane][y1>>2], s->bits[plane][y1>>2]);\
508  put_bits(&s->pb, 2, y1&3);
509 
510  if (s->bps <= 8) {
511  if (s->flags & AV_CODEC_FLAG_PASS1) {
512  for (i = 0; i < count; i++) {
513  LOAD2;
514  STAT2;
515  }
516  if (width&1) {
517  LOADEND;
518  STATEND;
519  }
520  }
521  if (s->avctx->flags2 & AV_CODEC_FLAG2_NO_OUTPUT)
522  return 0;
523 
524  if (s->context) {
525  for (i = 0; i < count; i++) {
526  LOAD2;
527  STAT2;
528  WRITE2;
529  }
530  if (width&1) {
531  LOADEND;
532  STATEND;
533  WRITEEND;
534  }
535  } else {
536  for (i = 0; i < count; i++) {
537  LOAD2;
538  WRITE2;
539  }
540  if (width&1) {
541  LOADEND;
542  WRITEEND;
543  }
544  }
545  } else if (s->bps <= 14) {
546  int mask = s->n - 1;
547  if (s->flags & AV_CODEC_FLAG_PASS1) {
548  for (i = 0; i < count; i++) {
549  LOAD2_14;
550  STAT2;
551  }
552  if (width&1) {
553  LOADEND_14;
554  STATEND;
555  }
556  }
557  if (s->avctx->flags2 & AV_CODEC_FLAG2_NO_OUTPUT)
558  return 0;
559 
560  if (s->context) {
561  for (i = 0; i < count; i++) {
562  LOAD2_14;
563  STAT2;
564  WRITE2;
565  }
566  if (width&1) {
567  LOADEND_14;
568  STATEND;
569  WRITEEND;
570  }
571  } else {
572  for (i = 0; i < count; i++) {
573  LOAD2_14;
574  WRITE2;
575  }
576  if (width&1) {
577  LOADEND_14;
578  WRITEEND;
579  }
580  }
581  } else {
582  if (s->flags & AV_CODEC_FLAG_PASS1) {
583  for (i = 0; i < count; i++) {
584  LOAD2_16;
585  STAT2_16;
586  }
587  if (width&1) {
588  LOADEND_16;
589  STATEND_16;
590  }
591  }
592  if (s->avctx->flags2 & AV_CODEC_FLAG2_NO_OUTPUT)
593  return 0;
594 
595  if (s->context) {
596  for (i = 0; i < count; i++) {
597  LOAD2_16;
598  STAT2_16;
599  WRITE2_16;
600  }
601  if (width&1) {
602  LOADEND_16;
603  STATEND_16;
604  WRITEEND_16;
605  }
606  } else {
607  for (i = 0; i < count; i++) {
608  LOAD2_16;
609  WRITE2_16;
610  }
611  if (width&1) {
612  LOADEND_16;
613  WRITEEND_16;
614  }
615  }
616  }
617 #undef LOAD2
618 #undef STAT2
619 #undef WRITE2
620  return 0;
621 }
622 
623 static int encode_gray_bitstream(HYuvContext *s, int count)
624 {
625  int i;
626 
627  if (put_bytes_left(&s->pb, 0) < 4 * count) {
628  av_log(s->avctx, AV_LOG_ERROR, "encoded frame too large\n");
629  return -1;
630  }
631 
632 #define LOAD2\
633  int y0 = s->temp[0][2 * i];\
634  int y1 = s->temp[0][2 * i + 1];
635 #define STAT2\
636  s->stats[0][y0]++;\
637  s->stats[0][y1]++;
638 #define WRITE2\
639  put_bits(&s->pb, s->len[0][y0], s->bits[0][y0]);\
640  put_bits(&s->pb, s->len[0][y1], s->bits[0][y1]);
641 
642  count /= 2;
643 
644  if (s->flags & AV_CODEC_FLAG_PASS1) {
645  for (i = 0; i < count; i++) {
646  LOAD2;
647  STAT2;
648  }
649  }
650  if (s->avctx->flags2 & AV_CODEC_FLAG2_NO_OUTPUT)
651  return 0;
652 
653  if (s->context) {
654  for (i = 0; i < count; i++) {
655  LOAD2;
656  STAT2;
657  WRITE2;
658  }
659  } else {
660  for (i = 0; i < count; i++) {
661  LOAD2;
662  WRITE2;
663  }
664  }
665  return 0;
666 }
667 
668 static inline int encode_bgra_bitstream(HYuvContext *s, int count, int planes)
669 {
670  int i;
671 
672  if (put_bytes_left(&s->pb, 0) < 4 * planes * count) {
673  av_log(s->avctx, AV_LOG_ERROR, "encoded frame too large\n");
674  return -1;
675  }
676 
677 #define LOAD_GBRA \
678  int g = s->temp[0][planes == 3 ? 3 * i + 1 : 4 * i + G]; \
679  int b =(s->temp[0][planes == 3 ? 3 * i + 2 : 4 * i + B] - g) & 0xFF;\
680  int r =(s->temp[0][planes == 3 ? 3 * i + 0 : 4 * i + R] - g) & 0xFF;\
681  int a = s->temp[0][planes * i + A];
682 
683 #define STAT_BGRA \
684  s->stats[0][b]++; \
685  s->stats[1][g]++; \
686  s->stats[2][r]++; \
687  if (planes == 4) \
688  s->stats[2][a]++;
689 
690 #define WRITE_GBRA \
691  put_bits(&s->pb, s->len[1][g], s->bits[1][g]); \
692  put_bits(&s->pb, s->len[0][b], s->bits[0][b]); \
693  put_bits(&s->pb, s->len[2][r], s->bits[2][r]); \
694  if (planes == 4) \
695  put_bits(&s->pb, s->len[2][a], s->bits[2][a]);
696 
697  if ((s->flags & AV_CODEC_FLAG_PASS1) &&
698  (s->avctx->flags2 & AV_CODEC_FLAG2_NO_OUTPUT)) {
699  for (i = 0; i < count; i++) {
700  LOAD_GBRA;
701  STAT_BGRA;
702  }
703  } else if (s->context || (s->flags & AV_CODEC_FLAG_PASS1)) {
704  for (i = 0; i < count; i++) {
705  LOAD_GBRA;
706  STAT_BGRA;
707  WRITE_GBRA;
708  }
709  } else {
710  for (i = 0; i < count; i++) {
711  LOAD_GBRA;
712  WRITE_GBRA;
713  }
714  }
715  return 0;
716 }
717 
719  const AVFrame *pict, int *got_packet)
720 {
721  HYuvContext *s = avctx->priv_data;
722  const int width = s->width;
723  const int width2 = s->width>>1;
724  const int height = s->height;
725  const int fake_ystride = s->interlaced ? pict->linesize[0]*2 : pict->linesize[0];
726  const int fake_ustride = s->interlaced ? pict->linesize[1]*2 : pict->linesize[1];
727  const int fake_vstride = s->interlaced ? pict->linesize[2]*2 : pict->linesize[2];
728  const AVFrame * const p = pict;
729  int i, j, size = 0, ret;
730 
731  if ((ret = ff_alloc_packet2(avctx, pkt, width * height * 3 * 4 + AV_INPUT_BUFFER_MIN_SIZE, 0)) < 0)
732  return ret;
733 
734  if (s->context) {
736  if (size < 0)
737  return size;
738 
739  for (i = 0; i < 4; i++)
740  for (j = 0; j < s->vlc_n; j++)
741  s->stats[i][j] >>= 1;
742  }
743 
744  init_put_bits(&s->pb, pkt->data + size, pkt->size - size);
745 
746  if (avctx->pix_fmt == AV_PIX_FMT_YUV422P ||
747  avctx->pix_fmt == AV_PIX_FMT_YUV420P) {
748  int lefty, leftu, leftv, y, cy;
749 
750  put_bits(&s->pb, 8, leftv = p->data[2][0]);
751  put_bits(&s->pb, 8, lefty = p->data[0][1]);
752  put_bits(&s->pb, 8, leftu = p->data[1][0]);
753  put_bits(&s->pb, 8, p->data[0][0]);
754 
755  lefty = sub_left_prediction(s, s->temp[0], p->data[0], width , 0);
756  leftu = sub_left_prediction(s, s->temp[1], p->data[1], width2, 0);
757  leftv = sub_left_prediction(s, s->temp[2], p->data[2], width2, 0);
758 
760 
761  if (s->predictor==MEDIAN) {
762  int lefttopy, lefttopu, lefttopv;
763  cy = y = 1;
764  if (s->interlaced) {
765  lefty = sub_left_prediction(s, s->temp[0], p->data[0] + p->linesize[0], width , lefty);
766  leftu = sub_left_prediction(s, s->temp[1], p->data[1] + p->linesize[1], width2, leftu);
767  leftv = sub_left_prediction(s, s->temp[2], p->data[2] + p->linesize[2], width2, leftv);
768 
770  y++; cy++;
771  }
772 
773  lefty = sub_left_prediction(s, s->temp[0], p->data[0] + fake_ystride, 4, lefty);
774  leftu = sub_left_prediction(s, s->temp[1], p->data[1] + fake_ustride, 2, leftu);
775  leftv = sub_left_prediction(s, s->temp[2], p->data[2] + fake_vstride, 2, leftv);
776 
777  encode_422_bitstream(s, 0, 4);
778 
779  lefttopy = p->data[0][3];
780  lefttopu = p->data[1][1];
781  lefttopv = p->data[2][1];
782  s->llvidencdsp.sub_median_pred(s->temp[0], p->data[0] + 4, p->data[0] + fake_ystride + 4, width - 4, &lefty, &lefttopy);
783  s->llvidencdsp.sub_median_pred(s->temp[1], p->data[1] + 2, p->data[1] + fake_ustride + 2, width2 - 2, &leftu, &lefttopu);
784  s->llvidencdsp.sub_median_pred(s->temp[2], p->data[2] + 2, p->data[2] + fake_vstride + 2, width2 - 2, &leftv, &lefttopv);
785  encode_422_bitstream(s, 0, width - 4);
786  y++; cy++;
787 
788  for (; y < height; y++,cy++) {
789  uint8_t *ydst, *udst, *vdst;
790 
791  if (s->bitstream_bpp == 12) {
792  while (2 * cy > y) {
793  ydst = p->data[0] + p->linesize[0] * y;
794  s->llvidencdsp.sub_median_pred(s->temp[0], ydst - fake_ystride, ydst, width, &lefty, &lefttopy);
796  y++;
797  }
798  if (y >= height) break;
799  }
800  ydst = p->data[0] + p->linesize[0] * y;
801  udst = p->data[1] + p->linesize[1] * cy;
802  vdst = p->data[2] + p->linesize[2] * cy;
803 
804  s->llvidencdsp.sub_median_pred(s->temp[0], ydst - fake_ystride, ydst, width, &lefty, &lefttopy);
805  s->llvidencdsp.sub_median_pred(s->temp[1], udst - fake_ustride, udst, width2, &leftu, &lefttopu);
806  s->llvidencdsp.sub_median_pred(s->temp[2], vdst - fake_vstride, vdst, width2, &leftv, &lefttopv);
807 
809  }
810  } else {
811  for (cy = y = 1; y < height; y++, cy++) {
812  uint8_t *ydst, *udst, *vdst;
813 
814  /* encode a luma only line & y++ */
815  if (s->bitstream_bpp == 12) {
816  ydst = p->data[0] + p->linesize[0] * y;
817 
818  if (s->predictor == PLANE && s->interlaced < y) {
819  s->llvidencdsp.diff_bytes(s->temp[1], ydst, ydst - fake_ystride, width);
820 
821  lefty = sub_left_prediction(s, s->temp[0], s->temp[1], width , lefty);
822  } else {
823  lefty = sub_left_prediction(s, s->temp[0], ydst, width , lefty);
824  }
826  y++;
827  if (y >= height) break;
828  }
829 
830  ydst = p->data[0] + p->linesize[0] * y;
831  udst = p->data[1] + p->linesize[1] * cy;
832  vdst = p->data[2] + p->linesize[2] * cy;
833 
834  if (s->predictor == PLANE && s->interlaced < cy) {
835  s->llvidencdsp.diff_bytes(s->temp[1], ydst, ydst - fake_ystride, width);
836  s->llvidencdsp.diff_bytes(s->temp[2], udst, udst - fake_ustride, width2);
837  s->llvidencdsp.diff_bytes(s->temp[2] + width2, vdst, vdst - fake_vstride, width2);
838 
839  lefty = sub_left_prediction(s, s->temp[0], s->temp[1], width , lefty);
840  leftu = sub_left_prediction(s, s->temp[1], s->temp[2], width2, leftu);
841  leftv = sub_left_prediction(s, s->temp[2], s->temp[2] + width2, width2, leftv);
842  } else {
843  lefty = sub_left_prediction(s, s->temp[0], ydst, width , lefty);
844  leftu = sub_left_prediction(s, s->temp[1], udst, width2, leftu);
845  leftv = sub_left_prediction(s, s->temp[2], vdst, width2, leftv);
846  }
847 
849  }
850  }
851  } else if(avctx->pix_fmt == AV_PIX_FMT_RGB32) {
852  uint8_t *data = p->data[0] + (height - 1) * p->linesize[0];
853  const int stride = -p->linesize[0];
854  const int fake_stride = -fake_ystride;
855  int y;
856  int leftr, leftg, leftb, lefta;
857 
858  put_bits(&s->pb, 8, lefta = data[A]);
859  put_bits(&s->pb, 8, leftr = data[R]);
860  put_bits(&s->pb, 8, leftg = data[G]);
861  put_bits(&s->pb, 8, leftb = data[B]);
862 
863  sub_left_prediction_bgr32(s, s->temp[0], data + 4, width - 1,
864  &leftr, &leftg, &leftb, &lefta);
865  encode_bgra_bitstream(s, width - 1, 4);
866 
867  for (y = 1; y < s->height; y++) {
868  uint8_t *dst = data + y*stride;
869  if (s->predictor == PLANE && s->interlaced < y) {
870  s->llvidencdsp.diff_bytes(s->temp[1], dst, dst - fake_stride, width * 4);
871  sub_left_prediction_bgr32(s, s->temp[0], s->temp[1], width,
872  &leftr, &leftg, &leftb, &lefta);
873  } else {
874  sub_left_prediction_bgr32(s, s->temp[0], dst, width,
875  &leftr, &leftg, &leftb, &lefta);
876  }
878  }
879  } else if (avctx->pix_fmt == AV_PIX_FMT_RGB24) {
880  uint8_t *data = p->data[0] + (height - 1) * p->linesize[0];
881  const int stride = -p->linesize[0];
882  const int fake_stride = -fake_ystride;
883  int y;
884  int leftr, leftg, leftb;
885 
886  put_bits(&s->pb, 8, leftr = data[0]);
887  put_bits(&s->pb, 8, leftg = data[1]);
888  put_bits(&s->pb, 8, leftb = data[2]);
889  put_bits(&s->pb, 8, 0);
890 
891  sub_left_prediction_rgb24(s, s->temp[0], data + 3, width - 1,
892  &leftr, &leftg, &leftb);
894 
895  for (y = 1; y < s->height; y++) {
896  uint8_t *dst = data + y * stride;
897  if (s->predictor == PLANE && s->interlaced < y) {
898  s->llvidencdsp.diff_bytes(s->temp[1], dst, dst - fake_stride,
899  width * 3);
900  sub_left_prediction_rgb24(s, s->temp[0], s->temp[1], width,
901  &leftr, &leftg, &leftb);
902  } else {
903  sub_left_prediction_rgb24(s, s->temp[0], dst, width,
904  &leftr, &leftg, &leftb);
905  }
907  }
908  } else if (s->version > 2) {
909  int plane;
910  for (plane = 0; plane < 1 + 2*s->chroma + s->alpha; plane++) {
911  int left, y;
912  int w = width;
913  int h = height;
914  int fake_stride = fake_ystride;
915 
916  if (s->chroma && (plane == 1 || plane == 2)) {
917  w >>= s->chroma_h_shift;
918  h >>= s->chroma_v_shift;
919  fake_stride = plane == 1 ? fake_ustride : fake_vstride;
920  }
921 
922  left = sub_left_prediction(s, s->temp[0], p->data[plane], w , 0);
923 
924  encode_plane_bitstream(s, w, plane);
925 
926  if (s->predictor==MEDIAN) {
927  int lefttop;
928  y = 1;
929  if (s->interlaced) {
930  left = sub_left_prediction(s, s->temp[0], p->data[plane] + p->linesize[plane], w , left);
931 
932  encode_plane_bitstream(s, w, plane);
933  y++;
934  }
935 
936  lefttop = p->data[plane][0];
937 
938  for (; y < h; y++) {
939  uint8_t *dst = p->data[plane] + p->linesize[plane] * y;
940 
941  sub_median_prediction(s, s->temp[0], dst - fake_stride, dst, w , &left, &lefttop);
942 
943  encode_plane_bitstream(s, w, plane);
944  }
945  } else {
946  for (y = 1; y < h; y++) {
947  uint8_t *dst = p->data[plane] + p->linesize[plane] * y;
948 
949  if (s->predictor == PLANE && s->interlaced < y) {
950  diff_bytes(s, s->temp[1], dst, dst - fake_stride, w);
951 
952  left = sub_left_prediction(s, s->temp[0], s->temp[1], w , left);
953  } else {
954  left = sub_left_prediction(s, s->temp[0], dst, w , left);
955  }
956 
957  encode_plane_bitstream(s, w, plane);
958  }
959  }
960  }
961  } else {
962  av_log(avctx, AV_LOG_ERROR, "Format not supported!\n");
963  }
964  emms_c();
965 
966  size += (put_bits_count(&s->pb) + 31) / 8;
967  put_bits(&s->pb, 16, 0);
968  put_bits(&s->pb, 15, 0);
969  size /= 4;
970 
971  if ((s->flags & AV_CODEC_FLAG_PASS1) && (s->picture_number & 31) == 0) {
972  int j;
973  char *p = avctx->stats_out;
974  char *end = p + STATS_OUT_SIZE;
975  for (i = 0; i < 4; i++) {
976  for (j = 0; j < s->vlc_n; j++) {
977  snprintf(p, end-p, "%"PRIu64" ", s->stats[i][j]);
978  p += strlen(p);
979  s->stats[i][j]= 0;
980  }
981  snprintf(p, end-p, "\n");
982  p++;
983  if (end <= p)
984  return AVERROR(ENOMEM);
985  }
986  } else if (avctx->stats_out)
987  avctx->stats_out[0] = '\0';
988  if (!(s->avctx->flags2 & AV_CODEC_FLAG2_NO_OUTPUT)) {
989  flush_put_bits(&s->pb);
990  s->bdsp.bswap_buf((uint32_t *) pkt->data, (uint32_t *) pkt->data, size);
991  }
992 
993  s->picture_number++;
994 
995  pkt->size = size * 4;
997  *got_packet = 1;
998 
999  return 0;
1000 }
1001 
1003 {
1004  HYuvContext *s = avctx->priv_data;
1005 
1007 
1008  av_freep(&avctx->stats_out);
1009 
1010  return 0;
1011 }
1012 
1013 #define OFFSET(x) offsetof(HYuvContext, x)
1014 #define VE AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_ENCODING_PARAM
1015 
1016 #define COMMON_OPTIONS \
1017  { "non_deterministic", "Allow multithreading for e.g. context=1 at the expense of determinism", \
1018  OFFSET(non_determ), AV_OPT_TYPE_BOOL, { .i64 = 1 }, \
1019  0, 1, VE }, \
1020  { "pred", "Prediction method", OFFSET(predictor), AV_OPT_TYPE_INT, { .i64 = LEFT }, LEFT, MEDIAN, VE, "pred" }, \
1021  { "left", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = LEFT }, INT_MIN, INT_MAX, VE, "pred" }, \
1022  { "plane", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = PLANE }, INT_MIN, INT_MAX, VE, "pred" }, \
1023  { "median", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = MEDIAN }, INT_MIN, INT_MAX, VE, "pred" }, \
1024 
1025 static const AVOption normal_options[] = {
1027  { NULL },
1028 };
1029 
1030 static const AVOption ff_options[] = {
1032  { "context", "Set per-frame huffman tables", OFFSET(context), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, 1, VE },
1033  { NULL },
1034 };
1035 
1036 static const AVClass normal_class = {
1037  .class_name = "huffyuv",
1038  .item_name = av_default_item_name,
1039  .option = normal_options,
1040  .version = LIBAVUTIL_VERSION_INT,
1041 };
1042 
1043 static const AVClass ff_class = {
1044  .class_name = "ffvhuff",
1045  .item_name = av_default_item_name,
1046  .option = ff_options,
1047  .version = LIBAVUTIL_VERSION_INT,
1048 };
1049 
1051  .name = "huffyuv",
1052  .long_name = NULL_IF_CONFIG_SMALL("Huffyuv / HuffYUV"),
1053  .type = AVMEDIA_TYPE_VIDEO,
1054  .id = AV_CODEC_ID_HUFFYUV,
1055  .priv_data_size = sizeof(HYuvContext),
1056  .init = encode_init,
1057  .encode2 = encode_frame,
1058  .close = encode_end,
1059  .capabilities = AV_CODEC_CAP_FRAME_THREADS,
1060  .priv_class = &normal_class,
1061  .pix_fmts = (const enum AVPixelFormat[]){
1064  },
1065  .caps_internal = FF_CODEC_CAP_INIT_THREADSAFE |
1067 };
1068 
1069 #if CONFIG_FFVHUFF_ENCODER
1070 const AVCodec ff_ffvhuff_encoder = {
1071  .name = "ffvhuff",
1072  .long_name = NULL_IF_CONFIG_SMALL("Huffyuv FFmpeg variant"),
1073  .type = AVMEDIA_TYPE_VIDEO,
1074  .id = AV_CODEC_ID_FFVHUFF,
1075  .priv_data_size = sizeof(HYuvContext),
1076  .init = encode_init,
1077  .encode2 = encode_frame,
1078  .close = encode_end,
1079  .capabilities = AV_CODEC_CAP_FRAME_THREADS,
1080  .priv_class = &ff_class,
1081  .pix_fmts = (const enum AVPixelFormat[]){
1097  },
1098  .caps_internal = FF_CODEC_CAP_INIT_THREADSAFE |
1100 };
1101 #endif
STATEND_16
#define STATEND_16
AV_PIX_FMT_YUVA422P16
#define AV_PIX_FMT_YUVA422P16
Definition: pixfmt.h:432
HYuvContext
Definition: huffyuv.h:55
AVCodec
AVCodec.
Definition: codec.h:197
stride
int stride
Definition: mace.c:144
FF_CODEC_CAP_INIT_THREADSAFE
#define FF_CODEC_CAP_INIT_THREADSAFE
The codec does not modify any global variables in the init function, allowing to call the init functi...
Definition: internal.h:41
AVPixelFormat
AVPixelFormat
Pixel format.
Definition: pixfmt.h:64
init
static av_cold int init(AVCodecContext *avctx)
Definition: avrndec.c:31
sub_median_prediction
static void sub_median_prediction(HYuvContext *s, uint8_t *dst, const uint8_t *src1, const uint8_t *src2, int w, int *left, int *left_top)
Definition: huffyuvenc.c:146
r
const char * r
Definition: vf_curves.c:116
AVERROR
Filter the word “frame” indicates either a video frame or a group of audio as stored in an AVFrame structure Format for each input and each output the list of supported formats For video that means pixel format For audio that means channel sample they are references to shared objects When the negotiation mechanism computes the intersection of the formats supported at each end of a all references to both lists are replaced with a reference to the intersection And when a single format is eventually chosen for a link amongst the remaining all references to the list are updated That means that if a filter requires that its input and output have the same format amongst a supported all it has to do is use a reference to the same list of formats query_formats can leave some formats unset and return AVERROR(EAGAIN) to cause the negotiation mechanism toagain later. That can be used by filters with complex requirements to use the format negotiated on one link to set the formats supported on another. Frame references ownership and permissions
opt.h
ff_huffyuvencdsp_init
av_cold void ff_huffyuvencdsp_init(HuffYUVEncDSPContext *c, AVCodecContext *avctx)
Definition: huffyuvencdsp.c:71
WRITE2_16
#define WRITE2_16
AV_CODEC_ID_HUFFYUV
@ AV_CODEC_ID_HUFFYUV
Definition: codec_id.h:74
FF_COMPLIANCE_EXPERIMENTAL
#define FF_COMPLIANCE_EXPERIMENTAL
Allow nonstandardized experimental things.
Definition: avcodec.h:1391
u
#define u(width, name, range_min, range_max)
Definition: cbs_h2645.c:264
av_pix_fmt_desc_get
const AVPixFmtDescriptor * av_pix_fmt_desc_get(enum AVPixelFormat pix_fmt)
Definition: pixdesc.c:2541
MAX_VLC_N
#define MAX_VLC_N
Definition: huffyuv.h:47
init_put_bits
static void init_put_bits(PutBitContext *s, uint8_t *buffer, int buffer_size)
Initialize the PutBitContext s.
Definition: put_bits.h:61
OFFSET
#define OFFSET(x)
Definition: huffyuvenc.c:1013
encode_init
static av_cold int encode_init(AVCodecContext *avctx)
Definition: huffyuvenc.c:202
AV_PIX_FMT_YUVA422P9
#define AV_PIX_FMT_YUVA422P9
Definition: pixfmt.h:424
AVFrame
This structure describes decoded (raw) audio or video data.
Definition: frame.h:303
put_bits
static void put_bits(Jpeg2000EncoderContext *s, int val, int n)
put n times val bit
Definition: j2kenc.c:219
pixdesc.h
index
fg index
Definition: ffmpeg_filter.c:168
AV_PIX_FMT_YUVA420P16
#define AV_PIX_FMT_YUVA420P16
Definition: pixfmt.h:431
w
uint8_t w
Definition: llviddspenc.c:39
ff_class
static const AVClass ff_class
Definition: huffyuvenc.c:1043
R
#define R
Definition: huffyuvdsp.h:34
internal.h
AVPacket::data
uint8_t * data
Definition: packet.h:365
ff_ffvhuff_encoder
const AVCodec ff_ffvhuff_encoder
AV_PIX_FMT_YUVA420P10
#define AV_PIX_FMT_YUVA420P10
Definition: pixfmt.h:426
AVOption
AVOption.
Definition: opt.h:248
b
#define b
Definition: input.c:41
encode_gray_bitstream
static int encode_gray_bitstream(HYuvContext *s, int count)
Definition: huffyuvenc.c:623
data
const char data[16]
Definition: mxf.c:142
AV_PIX_FMT_YUV420P10
#define AV_PIX_FMT_YUV420P10
Definition: pixfmt.h:389
STATS_OUT_SIZE
#define STATS_OUT_SIZE
MEDIAN
@ MEDIAN
Definition: huffyuv.h:52
WRITEEND_16
#define WRITEEND_16
AV_PIX_FMT_YUV440P
@ AV_PIX_FMT_YUV440P
planar YUV 4:4:0 (1 Cr & Cb sample per 1x2 Y samples)
Definition: pixfmt.h:99
AV_CODEC_FLAG_INTERLACED_ME
#define AV_CODEC_FLAG_INTERLACED_ME
interlaced motion estimation
Definition: avcodec.h:319
AV_PIX_FMT_YUVA422P10
#define AV_PIX_FMT_YUVA422P10
Definition: pixfmt.h:427
AV_PKT_FLAG_KEY
#define AV_PKT_FLAG_KEY
The packet contains a keyframe.
Definition: packet.h:396
AVFrame::data
uint8_t * data[AV_NUM_DATA_POINTERS]
pointer to the picture/channel planes.
Definition: frame.h:317
ff_huffyuv_alloc_temp
av_cold int ff_huffyuv_alloc_temp(HYuvContext *s)
Definition: huffyuv.c:58
LOADEND_14
#define LOADEND_14
STAT2_16
#define STAT2_16
A
#define A(x)
Definition: vp56_arith.h:28
AV_PIX_FMT_YUVA420P9
#define AV_PIX_FMT_YUVA420P9
Definition: pixfmt.h:423
AV_PIX_FMT_GBRP14
#define AV_PIX_FMT_GBRP14
Definition: pixfmt.h:407
AV_PIX_FMT_GBRAP
@ AV_PIX_FMT_GBRAP
planar GBRA 4:4:4:4 32bpp
Definition: pixfmt.h:205
v0
#define v0
Definition: regdef.h:26
AVCodecContext::codec
const struct AVCodec * codec
Definition: avcodec.h:510
sub_left_prediction
static int sub_left_prediction(HYuvContext *s, uint8_t *dst, const uint8_t *src, int w, int left)
Definition: huffyuvenc.c:51
AV_PIX_FMT_GBRP10
#define AV_PIX_FMT_GBRP10
Definition: pixfmt.h:405
AV_PIX_FMT_YUVA444P16
#define AV_PIX_FMT_YUVA444P16
Definition: pixfmt.h:433
AV_PIX_FMT_YUV422P9
#define AV_PIX_FMT_YUV422P9
Definition: pixfmt.h:387
LOAD2
#define LOAD2
AVCodecContext::flags
int flags
AV_CODEC_FLAG_*.
Definition: avcodec.h:581
val
static double val(void *priv, double ch)
Definition: aeval.c:76
av_pix_fmt_get_chroma_sub_sample
int av_pix_fmt_get_chroma_sub_sample(enum AVPixelFormat pix_fmt, int *h_shift, int *v_shift)
Utility function to access log2_chroma_w log2_chroma_h from the pixel format AVPixFmtDescriptor.
Definition: pixdesc.c:2569
AV_PIX_FMT_GRAY16
#define AV_PIX_FMT_GRAY16
Definition: pixfmt.h:373
AV_CODEC_FLAG2_NO_OUTPUT
#define AV_CODEC_FLAG2_NO_OUTPUT
Skip bitstream encoding.
Definition: avcodec.h:329
put_bytes_left
static int put_bytes_left(const PutBitContext *s, int round_up)
Definition: put_bits.h:134
AV_PIX_FMT_YUV444P10
#define AV_PIX_FMT_YUV444P10
Definition: pixfmt.h:392
store_table
static int store_table(HYuvContext *s, const uint8_t *len, uint8_t *buf)
Definition: huffyuvenc.c:155
pkt
AVPacket * pkt
Definition: movenc.c:59
AV_LOG_ERROR
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:181
av_cold
#define av_cold
Definition: attributes.h:90
AV_PIX_FMT_YUV422P16
#define AV_PIX_FMT_YUV422P16
Definition: pixfmt.h:401
ff_huffyuv_encoder
const AVCodec ff_huffyuv_encoder
Definition: huffyuvenc.c:1050
mask
static const uint16_t mask[17]
Definition: lzw.c:38
sub_left_prediction_bgr32
static void sub_left_prediction_bgr32(HYuvContext *s, uint8_t *dst, const uint8_t *src, int w, int *red, int *green, int *blue, int *alpha)
Definition: huffyuvenc.c:82
STATEND
#define STATEND
WRITE2
#define WRITE2
width
#define width
s
#define s(width, name)
Definition: cbs_vp9.c:257
AVCodecContext::stats_in
char * stats_in
pass2 encoding statistics input buffer Concatenated stuff from stats_out of pass1 should be placed he...
Definition: avcodec.h:1350
AV_PIX_FMT_YUVA420P
@ AV_PIX_FMT_YUVA420P
planar YUV 4:2:0, 20bpp, (1 Cr & Cb sample per 2x2 Y & A samples)
Definition: pixfmt.h:101
AV_PIX_FMT_YUV444P16
#define AV_PIX_FMT_YUV444P16
Definition: pixfmt.h:402
g
const char * g
Definition: vf_curves.c:117
sub_left_prediction_rgb24
static void sub_left_prediction_rgb24(HYuvContext *s, uint8_t *dst, uint8_t *src, int w, int *red, int *green, int *blue)
Definition: huffyuvenc.c:118
STAT2
#define STAT2
av_assert0
#define av_assert0(cond)
assert() equivalent, that is always enabled.
Definition: avassert.h:37
pix_fmts
static enum AVPixelFormat pix_fmts[]
Definition: libkvazaar.c:289
normal_class
static const AVClass normal_class
Definition: huffyuvenc.c:1036
AV_PIX_FMT_YUV420P9
#define AV_PIX_FMT_YUV420P9
Definition: pixfmt.h:386
AV_PIX_FMT_YUV420P16
#define AV_PIX_FMT_YUV420P16
Definition: pixfmt.h:400
AV_PIX_FMT_FLAG_ALPHA
#define AV_PIX_FMT_FLAG_ALPHA
The pixel format has an alpha channel.
Definition: pixdesc.h:148
AV_INPUT_BUFFER_MIN_SIZE
#define AV_INPUT_BUFFER_MIN_SIZE
Definition: avcodec.h:199
huffyuvencdsp.h
AV_CODEC_ID_FFVHUFF
@ AV_CODEC_ID_FFVHUFF
Definition: codec_id.h:116
AV_PIX_FMT_YUV420P
@ AV_PIX_FMT_YUV420P
planar YUV 4:2:0, 12bpp, (1 Cr & Cb sample per 2x2 Y samples)
Definition: pixfmt.h:66
WRITE_GBRA
#define WRITE_GBRA
MAX_N
#define MAX_N
Definition: huffyuv.h:46
LOADEND_16
#define LOADEND_16
ff_huffyuv_common_end
av_cold void ff_huffyuv_common_end(HYuvContext *s)
Definition: huffyuv.c:86
context
it s the only field you need to keep assuming you have a context There is some magic you don t need to care about around this just let it vf default minimum maximum flags name is the option keep it simple and lowercase description are in without and describe what they for example set the foo of the bar offset is the offset of the field in your context
Definition: writing_filters.txt:91
AV_CODEC_CAP_FRAME_THREADS
#define AV_CODEC_CAP_FRAME_THREADS
Codec supports frame-level multithreading.
Definition: codec.h:108
AV_PIX_FMT_GBRP16
#define AV_PIX_FMT_GBRP16
Definition: pixfmt.h:408
LIBAVUTIL_VERSION_INT
#define LIBAVUTIL_VERSION_INT
Definition: version.h:85
LOADEND
#define LOADEND
AVClass
Describe the class of an AVClass context structure.
Definition: log.h:67
NULL
#define NULL
Definition: coverity.c:32
store_huffman_tables
static int store_huffman_tables(HYuvContext *s, uint8_t *buf)
Definition: huffyuvenc.c:180
WRITEEND
#define WRITEEND
av_default_item_name
const char * av_default_item_name(void *ptr)
Return the context name.
Definition: log.c:235
src
#define src
Definition: vp8dsp.c:255
ff_huff_gen_len_table
int ff_huff_gen_len_table(uint8_t *dst, const uint64_t *stats, int stats_size, int skip0)
Definition: huffman.c:58
ff_huffyuv_generate_bits_table
int ff_huffyuv_generate_bits_table(uint32_t *dst, const uint8_t *len_table, int n)
Definition: huffyuv.c:39
AV_PIX_FMT_YUV422P10
#define AV_PIX_FMT_YUV422P10
Definition: pixfmt.h:390
AV_PIX_FMT_GRAY8
@ AV_PIX_FMT_GRAY8
Y , 8bpp.
Definition: pixfmt.h:74
AV_PIX_FMT_GBRP9
#define AV_PIX_FMT_GBRP9
Definition: pixfmt.h:404
diff_bytes
static void diff_bytes(HYuvContext *s, uint8_t *dst, const uint8_t *src0, const uint8_t *src1, int w)
Definition: huffyuvenc.c:41
encode_end
static av_cold int encode_end(AVCodecContext *avctx)
Definition: huffyuvenc.c:1002
ff_llvidencdsp_init
av_cold void ff_llvidencdsp_init(LLVidEncDSPContext *c)
Definition: lossless_videoencdsp.c:91
for
for(j=16;j >0;--j)
Definition: h264pred_template.c:469
LOAD2_16
#define LOAD2_16
AVCodecContext::stats_out
char * stats_out
pass1 encoding statistics output buffer
Definition: avcodec.h:1342
AV_PIX_FMT_RGB24
@ AV_PIX_FMT_RGB24
packed RGB 8:8:8, 24bpp, RGBRGB...
Definition: pixfmt.h:68
AVPacket::size
int size
Definition: packet.h:366
NULL_IF_CONFIG_SMALL
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification.
Definition: internal.h:117
LOAD4
#define LOAD4
AV_PIX_FMT_FLAG_RGB
#define AV_PIX_FMT_FLAG_RGB
The pixel format contains RGB-like data (as opposed to YUV/grayscale).
Definition: pixdesc.h:137
AV_PIX_FMT_YUV422P12
#define AV_PIX_FMT_YUV422P12
Definition: pixfmt.h:394
size
int size
Definition: twinvq_data.h:10344
AV_PIX_FMT_YUV444P12
#define AV_PIX_FMT_YUV444P12
Definition: pixfmt.h:396
AV_CODEC_FLAG_PASS2
#define AV_CODEC_FLAG_PASS2
Use internal 2pass ratecontrol in second pass mode.
Definition: avcodec.h:277
height
#define height
AV_PIX_FMT_RGB32
#define AV_PIX_FMT_RGB32
Definition: pixfmt.h:362
a
The reader does not expect b to be semantically here and if the code is changed by maybe adding a a division or other the signedness will almost certainly be mistaken To avoid this confusion a new type was SUINT is the C unsigned type but it holds a signed int to use the same example SUINT a
Definition: undefined.txt:41
FFMIN
#define FFMIN(a, b)
Definition: common.h:105
AV_PIX_FMT_YUVA444P
@ AV_PIX_FMT_YUVA444P
planar YUV 4:4:4 32bpp, (1 Cr & Cb sample per 1x1 Y & A samples)
Definition: pixfmt.h:167
AV_PIX_FMT_YUVA444P10
#define AV_PIX_FMT_YUVA444P10
Definition: pixfmt.h:428
offset
it s the only field you need to keep assuming you have a context There is some magic you don t need to care about around this just let it vf offset
Definition: writing_filters.txt:86
AVPacket::flags
int flags
A combination of AV_PKT_FLAG values.
Definition: packet.h:371
COMMON_OPTIONS
#define COMMON_OPTIONS
Definition: huffyuvenc.c:1016
src0
#define src0
Definition: h264pred.c:139
AV_LOG_INFO
#define AV_LOG_INFO
Standard information.
Definition: log.h:192
VE
#define VE
Definition: huffyuvenc.c:1014
AVCodec::id
enum AVCodecID id
Definition: codec.h:211
lossless_videoencdsp.h
src1
#define src1
Definition: h264pred.c:140
AVCodecContext::bits_per_coded_sample
int bits_per_coded_sample
bits per sample/pixel from the demuxer (needed for huffyuv).
Definition: avcodec.h:1524
i
int i
Definition: input.c:407
put_bits_count
static int put_bits_count(PutBitContext *s)
Definition: put_bits.h:79
AVCodecContext::extradata
uint8_t * extradata
some codecs need / can use extradata like Huffman tables.
Definition: avcodec.h:602
encode_422_bitstream
static int encode_422_bitstream(HYuvContext *s, int offset, int count)
Definition: huffyuvenc.c:406
FF_CODEC_CAP_INIT_CLEANUP
#define FF_CODEC_CAP_INIT_CLEANUP
The codec allows calling the close function for deallocation even if the init function returned a fai...
Definition: internal.h:49
normal_options
static const AVOption normal_options[]
Definition: huffyuvenc.c:1025
AV_PIX_FMT_GBRP12
#define AV_PIX_FMT_GBRP12
Definition: pixfmt.h:406
av_mallocz
void * av_mallocz(size_t size)
Allocate a memory block with alignment suitable for all memory accesses (including vectors if availab...
Definition: mem.c:243
AVCodec::name
const char * name
Name of the codec implementation.
Definition: codec.h:204
len
int len
Definition: vorbis_enc_data.h:426
PLANE
@ PLANE
Definition: huffyuv.h:51
AVCodecContext::pix_fmt
enum AVPixelFormat pix_fmt
Pixel format, see AV_PIX_FMT_xxx.
Definition: avcodec.h:711
AV_PIX_FMT_YUV444P9
#define AV_PIX_FMT_YUV444P9
Definition: pixfmt.h:388
avcodec.h
G
#define G
Definition: huffyuvdsp.h:33
ret
ret
Definition: filter_design.txt:187
AVClass::class_name
const char * class_name
The name of the class; usually it is the same name as the context structure type to which the AVClass...
Definition: log.h:72
AVCodecContext::strict_std_compliance
int strict_std_compliance
strictly follow the standard (MPEG-4, ...).
Definition: avcodec.h:1386
AV_PIX_FMT_YUVA444P9
#define AV_PIX_FMT_YUVA444P9
Definition: pixfmt.h:425
AV_PIX_FMT_YUV420P12
#define AV_PIX_FMT_YUV420P12
Definition: pixfmt.h:393
left
Tag MUST be and< 10hcoeff half pel interpolation filter coefficients, hcoeff[0] are the 2 middle coefficients[1] are the next outer ones and so on, resulting in a filter like:...eff[2], hcoeff[1], hcoeff[0], hcoeff[0], hcoeff[1], hcoeff[2] ... the sign of the coefficients is not explicitly stored but alternates after each coeff and coeff[0] is positive, so ...,+,-,+,-,+,+,-,+,-,+,... hcoeff[0] is not explicitly stored but found by subtracting the sum of all stored coefficients with signs from 32 hcoeff[0]=32 - hcoeff[1] - hcoeff[2] - ... a good choice for hcoeff and htaps is htaps=6 hcoeff={40,-10, 2} an alternative which requires more computations at both encoder and decoder side and may or may not be better is htaps=8 hcoeff={42,-14, 6,-2}ref_frames minimum of the number of available reference frames and max_ref_frames for example the first frame after a key frame always has ref_frames=1spatial_decomposition_type wavelet type 0 is a 9/7 symmetric compact integer wavelet 1 is a 5/3 symmetric compact integer wavelet others are reserved stored as delta from last, last is reset to 0 if always_reset||keyframeqlog quality(logarithmic quantizer scale) stored as delta from last, last is reset to 0 if always_reset||keyframemv_scale stored as delta from last, last is reset to 0 if always_reset||keyframe FIXME check that everything works fine if this changes between framesqbias dequantization bias stored as delta from last, last is reset to 0 if always_reset||keyframeblock_max_depth maximum depth of the block tree stored as delta from last, last is reset to 0 if always_reset||keyframequant_table quantization tableHighlevel bitstream structure:==============================--------------------------------------------|Header|--------------------------------------------|------------------------------------|||Block0||||split?||||yes no||||......... intra?||||:Block01 :yes no||||:Block02 :....... ..........||||:Block03 ::y DC ::ref index:||||:Block04 ::cb DC ::motion x :||||......... :cr DC ::motion y :||||....... ..........|||------------------------------------||------------------------------------|||Block1|||...|--------------------------------------------|------------ ------------ ------------|||Y subbands||Cb subbands||Cr subbands||||--- ---||--- ---||--- ---|||||LL0||HL0||||LL0||HL0||||LL0||HL0|||||--- ---||--- ---||--- ---||||--- ---||--- ---||--- ---|||||LH0||HH0||||LH0||HH0||||LH0||HH0|||||--- ---||--- ---||--- ---||||--- ---||--- ---||--- ---|||||HL1||LH1||||HL1||LH1||||HL1||LH1|||||--- ---||--- ---||--- ---||||--- ---||--- ---||--- ---|||||HH1||HL2||||HH1||HL2||||HH1||HL2|||||...||...||...|||------------ ------------ ------------|--------------------------------------------Decoding process:=================------------|||Subbands|------------||||------------|Intra DC||||LL0 subband prediction ------------|\ Dequantization ------------------- \||Reference frames|\ IDWT|------- -------|Motion \|||Frame 0||Frame 1||Compensation . OBMC v -------|------- -------|--------------. \------> Frame n output Frame Frame<----------------------------------/|...|------------------- Range Coder:============Binary Range Coder:------------------- The implemented range coder is an adapted version based upon "Range encoding: an algorithm for removing redundancy from a digitised message." by G. N. N. Martin. The symbols encoded by the Snow range coder are bits(0|1). The associated probabilities are not fix but change depending on the symbol mix seen so far. bit seen|new state ---------+----------------------------------------------- 0|256 - state_transition_table[256 - old_state];1|state_transition_table[old_state];state_transition_table={ 0, 0, 0, 0, 0, 0, 0, 0, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 94, 95, 96, 97, 98, 99, 100, 101, 102, 103, 104, 105, 106, 107, 108, 109, 110, 111, 112, 113, 114, 114, 115, 116, 117, 118, 119, 120, 121, 122, 123, 124, 125, 126, 127, 128, 129, 130, 131, 132, 133, 133, 134, 135, 136, 137, 138, 139, 140, 141, 142, 143, 144, 145, 146, 147, 148, 149, 150, 151, 152, 152, 153, 154, 155, 156, 157, 158, 159, 160, 161, 162, 163, 164, 165, 166, 167, 168, 169, 170, 171, 171, 172, 173, 174, 175, 176, 177, 178, 179, 180, 181, 182, 183, 184, 185, 186, 187, 188, 189, 190, 190, 191, 192, 194, 194, 195, 196, 197, 198, 199, 200, 201, 202, 202, 204, 205, 206, 207, 208, 209, 209, 210, 211, 212, 213, 215, 215, 216, 217, 218, 219, 220, 220, 222, 223, 224, 225, 226, 227, 227, 229, 229, 230, 231, 232, 234, 234, 235, 236, 237, 238, 239, 240, 241, 242, 243, 244, 245, 246, 247, 248, 248, 0, 0, 0, 0, 0, 0, 0};FIXME Range Coding of integers:------------------------- FIXME Neighboring Blocks:===================left and top are set to the respective blocks unless they are outside of the image in which case they are set to the Null block top-left is set to the top left block unless it is outside of the image in which case it is set to the left block if this block has no larger parent block or it is at the left side of its parent block and the top right block is not outside of the image then the top right block is used for top-right else the top-left block is used Null block y, cb, cr are 128 level, ref, mx and my are 0 Motion Vector Prediction:=========================1. the motion vectors of all the neighboring blocks are scaled to compensate for the difference of reference frames scaled_mv=(mv *(256 *(current_reference+1)/(mv.reference+1))+128)> the median of the scaled left
Definition: snow.txt:386
AV_PIX_FMT_YUV422P14
#define AV_PIX_FMT_YUV422P14
Definition: pixfmt.h:398
B
#define B
Definition: huffyuvdsp.h:32
AVCodecContext
main external API structure.
Definition: avcodec.h:501
LOAD_GBRA
#define LOAD_GBRA
AV_PIX_FMT_NONE
@ AV_PIX_FMT_NONE
Definition: pixfmt.h:65
AV_OPT_TYPE_INT
@ AV_OPT_TYPE_INT
Definition: opt.h:225
huffman.h
temp
else temp
Definition: vf_mcdeint.c:259
AV_PIX_FMT_FLAG_PLANAR
#define AV_PIX_FMT_FLAG_PLANAR
At least one pixel component is not in the first data plane.
Definition: pixdesc.h:133
ff_options
static const AVOption ff_options[]
Definition: huffyuvenc.c:1030
AV_PIX_FMT_YUV444P
@ AV_PIX_FMT_YUV444P
planar YUV 4:4:4, 24bpp, (1 Cr & Cb sample per 1x1 Y samples)
Definition: pixfmt.h:71
encode_frame
static int encode_frame(AVCodecContext *avctx, AVPacket *pkt, const AVFrame *pict, int *got_packet)
Definition: huffyuvenc.c:718
AV_PIX_FMT_GBRP
@ AV_PIX_FMT_GBRP
planar GBR 4:4:4 24bpp
Definition: pixfmt.h:158
planes
static const struct @322 planes[]
desc
const char * desc
Definition: libsvtav1.c:79
AVMEDIA_TYPE_VIDEO
@ AVMEDIA_TYPE_VIDEO
Definition: avutil.h:201
AV_PIX_FMT_YUV422P
@ AV_PIX_FMT_YUV422P
planar YUV 4:2:2, 16bpp, (1 Cr & Cb sample per 2x1 Y samples)
Definition: pixfmt.h:70
LOAD2_14
#define LOAD2_14
flush_put_bits
static void flush_put_bits(PutBitContext *s)
Pad the end of the output stream with zeros.
Definition: put_bits.h:142
AVPixFmtDescriptor
Descriptor that unambiguously describes how the bits of a pixel are stored in the up to 4 data planes...
Definition: pixdesc.h:70
alpha
static const int16_t alpha[]
Definition: ilbcdata.h:55
ff_huffyuv_common_init
av_cold void ff_huffyuv_common_init(AVCodecContext *avctx)
Definition: huffyuv.c:71
AVPacket
This structure stores compressed data.
Definition: packet.h:342
AVCodecContext::priv_data
void * priv_data
Definition: avcodec.h:528
av_freep
#define av_freep(p)
Definition: tableprint_vlc.h:35
AV_PIX_FMT_YUV411P
@ AV_PIX_FMT_YUV411P
planar YUV 4:1:1, 12bpp, (1 Cr & Cb sample per 4x1 Y samples)
Definition: pixfmt.h:73
d
d
Definition: ffmpeg_filter.c:156
encode_bgra_bitstream
static int encode_bgra_bitstream(HYuvContext *s, int count, int planes)
Definition: huffyuvenc.c:668
AVFrame::linesize
int linesize[AV_NUM_DATA_POINTERS]
For video, size in bytes of each picture line.
Definition: frame.h:334
AV_PIX_FMT_YUV410P
@ AV_PIX_FMT_YUV410P
planar YUV 4:1:0, 9bpp, (1 Cr & Cb sample per 4x4 Y samples)
Definition: pixfmt.h:72
av_log
#define av_log(a,...)
Definition: tableprint_vlc.h:28
h
h
Definition: vp9dsp_template.c:2038
AV_PIX_FMT_YUV444P14
#define AV_PIX_FMT_YUV444P14
Definition: pixfmt.h:399
huffyuv.h
ff_alloc_packet2
int ff_alloc_packet2(AVCodecContext *avctx, AVPacket *avpkt, int64_t size, int64_t min_size)
Check AVPacket size and/or allocate data.
Definition: encode.c:33
put_bits.h
snprintf
#define snprintf
Definition: snprintf.h:34
encode_plane_bitstream
static int encode_plane_bitstream(HYuvContext *s, int width, int plane)
Definition: huffyuvenc.c:461
AV_PIX_FMT_YUVA422P
@ AV_PIX_FMT_YUVA422P
planar YUV 4:2:2 24bpp, (1 Cr & Cb sample per 2x1 Y & A samples)
Definition: pixfmt.h:166
AV_PIX_FMT_YUV420P14
#define AV_PIX_FMT_YUV420P14
Definition: pixfmt.h:397
STAT_BGRA
#define STAT_BGRA
AV_CODEC_FLAG_PASS1
#define AV_CODEC_FLAG_PASS1
Use internal 2pass ratecontrol in first pass mode.
Definition: avcodec.h:273