FFmpeg
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
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 "internal.h"
35 #include "put_bits.h"
36 #include "libavutil/pixdesc.h"
37 
38 static inline void diff_bytes(HYuvContext *s, uint8_t *dst,
39  const uint8_t *src0, const uint8_t *src1, int w)
40 {
41  if (s->bps <= 8) {
42  s->dsp.diff_bytes(dst, src0, src1, w);
43  } else {
44  s->llviddsp.diff_int16((uint16_t *)dst, (const uint16_t *)src0, (const uint16_t *)src1, s->n - 1, w);
45  }
46 }
47 
48 static inline int sub_left_prediction(HYuvContext *s, uint8_t *dst,
49  const uint8_t *src, int w, int left)
50 {
51  int i;
52  if (s->bps <= 8) {
53  if (w < 32) {
54  for (i = 0; i < w; i++) {
55  const int temp = src[i];
56  dst[i] = temp - left;
57  left = temp;
58  }
59  return left;
60  } else {
61  for (i = 0; i < 16; i++) {
62  const int temp = src[i];
63  dst[i] = temp - left;
64  left = temp;
65  }
66  s->dsp.diff_bytes(dst + 16, src + 16, src + 15, w - 16);
67  return src[w-1];
68  }
69  } else {
70  const uint16_t *src16 = (const uint16_t *)src;
71  uint16_t *dst16 = ( uint16_t *)dst;
72  if (w < 32) {
73  for (i = 0; i < w; i++) {
74  const int temp = src16[i];
75  dst16[i] = temp - left;
76  left = temp;
77  }
78  return left;
79  } else {
80  for (i = 0; i < 16; i++) {
81  const int temp = src16[i];
82  dst16[i] = temp - left;
83  left = temp;
84  }
85  s->llviddsp.diff_int16(dst16 + 16, src16 + 16, src16 + 15, s->n - 1, w - 16);
86  return src16[w-1];
87  }
88  }
89 }
90 
91 static inline void sub_left_prediction_bgr32(HYuvContext *s, uint8_t *dst,
92  const uint8_t *src, int w,
93  int *red, int *green, int *blue,
94  int *alpha)
95 {
96  int i;
97  int r, g, b, a;
98  r = *red;
99  g = *green;
100  b = *blue;
101  a = *alpha;
102 
103  for (i = 0; i < FFMIN(w, 4); i++) {
104  const int rt = src[i * 4 + R];
105  const int gt = src[i * 4 + G];
106  const int bt = src[i * 4 + B];
107  const int at = src[i * 4 + A];
108  dst[i * 4 + R] = rt - r;
109  dst[i * 4 + G] = gt - g;
110  dst[i * 4 + B] = bt - b;
111  dst[i * 4 + A] = at - a;
112  r = rt;
113  g = gt;
114  b = bt;
115  a = at;
116  }
117 
118  s->dsp.diff_bytes(dst + 16, src + 16, src + 12, w * 4 - 16);
119 
120  *red = src[(w - 1) * 4 + R];
121  *green = src[(w - 1) * 4 + G];
122  *blue = src[(w - 1) * 4 + B];
123  *alpha = src[(w - 1) * 4 + A];
124 }
125 
126 static inline void sub_left_prediction_rgb24(HYuvContext *s, uint8_t *dst,
127  uint8_t *src, int w,
128  int *red, int *green, int *blue)
129 {
130  int i;
131  int r, g, b;
132  r = *red;
133  g = *green;
134  b = *blue;
135  for (i = 0; i < FFMIN(w, 16); i++) {
136  const int rt = src[i * 3 + 0];
137  const int gt = src[i * 3 + 1];
138  const int bt = src[i * 3 + 2];
139  dst[i * 3 + 0] = rt - r;
140  dst[i * 3 + 1] = gt - g;
141  dst[i * 3 + 2] = bt - b;
142  r = rt;
143  g = gt;
144  b = bt;
145  }
146 
147  s->dsp.diff_bytes(dst + 48, src + 48, src + 48 - 3, w * 3 - 48);
148 
149  *red = src[(w - 1) * 3 + 0];
150  *green = src[(w - 1) * 3 + 1];
151  *blue = src[(w - 1) * 3 + 2];
152 }
153 
154 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)
155 {
156  if (s->bps <= 8) {
157  s->dsp.sub_hfyu_median_prediction(dst, src1, src2, w , left, left_top);
158  } else {
159  s->llviddsp.sub_hfyu_median_prediction_int16((uint16_t *)dst, (const uint16_t *)src1, (const uint16_t *)src2, s->n - 1, w , left, left_top);
160  }
161 }
162 
164 {
165  int i;
166  int index = 0;
167  int n = s->vlc_n;
168 
169  for (i = 0; i < n;) {
170  int val = len[i];
171  int repeat = 0;
172 
173  for (; i < n && len[i] == val && repeat < 255; i++)
174  repeat++;
175 
176  av_assert0(val < 32 && val >0 && repeat < 256 && repeat>0);
177  if (repeat > 7) {
178  buf[index++] = val;
179  buf[index++] = repeat;
180  } else {
181  buf[index++] = val | (repeat << 5);
182  }
183  }
184 
185  return index;
186 }
187 
189 {
190  int i, ret;
191  int size = 0;
192  int count = 3;
193 
194  if (s->version > 2)
195  count = 1 + s->alpha + 2*s->chroma;
196 
197  for (i = 0; i < count; i++) {
198  if ((ret = ff_huff_gen_len_table(s->len[i], s->stats[i], s->vlc_n)) < 0)
199  return ret;
200 
201  if (ff_huffyuv_generate_bits_table(s->bits[i], s->len[i], s->vlc_n) < 0) {
202  return -1;
203  }
204 
205  size += store_table(s, s->len[i], buf + size);
206  }
207  return size;
208 }
209 
211 {
212  HYuvContext *s = avctx->priv_data;
213  int i, j;
214  int ret;
215  const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(avctx->pix_fmt);
216 
217  ff_huffyuv_common_init(avctx);
218 
219  avctx->extradata = av_mallocz(3*MAX_N + 4);
220  if (!avctx->extradata)
221  return AVERROR(ENOMEM);
222  if (s->flags&CODEC_FLAG_PASS1) {
223 #define STATS_OUT_SIZE 21*MAX_N*3 + 4
224  avctx->stats_out = av_mallocz(STATS_OUT_SIZE); // 21*256*3(%llu ) + 3(\n) + 1(0) = 16132
225  if (!avctx->stats_out)
226  return AVERROR(ENOMEM);
227  }
228  s->version = 2;
229 
230  avctx->coded_frame = av_frame_alloc();
231  if (!avctx->coded_frame)
232  return AVERROR(ENOMEM);
233 
235  avctx->coded_frame->key_frame = 1;
236 
237  s->bps = desc->comp[0].depth_minus1 + 1;
238  s->yuv = !(desc->flags & AV_PIX_FMT_FLAG_RGB) && desc->nb_components >= 2;
239  s->chroma = desc->nb_components > 2;
240  s->alpha = !!(desc->flags & AV_PIX_FMT_FLAG_ALPHA);
242  &s->chroma_h_shift,
243  &s->chroma_v_shift);
244 
245  switch (avctx->pix_fmt) {
246  case AV_PIX_FMT_YUV420P:
247  case AV_PIX_FMT_YUV422P:
248  if (s->width & 1) {
249  av_log(avctx, AV_LOG_ERROR, "Width must be even for this colorspace.\n");
250  return AVERROR(EINVAL);
251  }
252  s->bitstream_bpp = avctx->pix_fmt == AV_PIX_FMT_YUV420P ? 12 : 16;
253  break;
254  case AV_PIX_FMT_YUV444P:
255  case AV_PIX_FMT_YUV410P:
256  case AV_PIX_FMT_YUV411P:
257  case AV_PIX_FMT_YUV440P:
258  case AV_PIX_FMT_GBRP:
259  case AV_PIX_FMT_GBRP9:
260  case AV_PIX_FMT_GBRP10:
261  case AV_PIX_FMT_GBRP12:
262  case AV_PIX_FMT_GBRP14:
263  case AV_PIX_FMT_GBRP16:
264  case AV_PIX_FMT_GRAY8:
265  case AV_PIX_FMT_GRAY16:
266  case AV_PIX_FMT_YUVA444P:
267  case AV_PIX_FMT_YUVA420P:
268  case AV_PIX_FMT_YUVA422P:
269  case AV_PIX_FMT_GBRAP:
270  case AV_PIX_FMT_GRAY8A:
271  case AV_PIX_FMT_YUV420P9:
276  case AV_PIX_FMT_YUV422P9:
281  case AV_PIX_FMT_YUV444P9:
295  s->version = 3;
296  break;
297  case AV_PIX_FMT_RGB32:
298  s->bitstream_bpp = 32;
299  break;
300  case AV_PIX_FMT_RGB24:
301  s->bitstream_bpp = 24;
302  break;
303  default:
304  av_log(avctx, AV_LOG_ERROR, "format not supported\n");
305  return AVERROR(EINVAL);
306  }
307  s->n = 1<<s->bps;
308  s->vlc_n = FFMIN(s->n, MAX_VLC_N);
309 
311  s->decorrelate = s->bitstream_bpp >= 24 && !s->yuv && !(desc->flags & AV_PIX_FMT_FLAG_PLANAR);
312  s->predictor = avctx->prediction_method;
313  s->interlaced = avctx->flags&CODEC_FLAG_INTERLACED_ME ? 1 : 0;
314  if (avctx->context_model == 1) {
315  s->context = avctx->context_model;
317  av_log(avctx, AV_LOG_ERROR,
318  "context=1 is not compatible with "
319  "2 pass huffyuv encoding\n");
320  return AVERROR(EINVAL);
321  }
322  }else s->context= 0;
323 
324  if (avctx->codec->id == AV_CODEC_ID_HUFFYUV) {
325  if (avctx->pix_fmt == AV_PIX_FMT_YUV420P) {
326  av_log(avctx, AV_LOG_ERROR,
327  "Error: YV12 is not supported by huffyuv; use "
328  "vcodec=ffvhuff or format=422p\n");
329  return AVERROR(EINVAL);
330  }
331  if (avctx->context_model) {
332  av_log(avctx, AV_LOG_ERROR,
333  "Error: per-frame huffman tables are not supported "
334  "by huffyuv; use vcodec=ffvhuff\n");
335  return AVERROR(EINVAL);
336  }
337  if (s->version > 2) {
338  av_log(avctx, AV_LOG_ERROR,
339  "Error: ver>2 is not supported "
340  "by huffyuv; use vcodec=ffvhuff\n");
341  return AVERROR(EINVAL);
342  }
343  if (s->interlaced != ( s->height > 288 ))
344  av_log(avctx, AV_LOG_INFO,
345  "using huffyuv 2.2.0 or newer interlacing flag\n");
346  }
347 
349  av_log(avctx, AV_LOG_ERROR, "Ver > 2 is under development, files encoded with it may not be decodable with future versions!!!\n"
350  "Use vstrict=-2 / -strict -2 to use it anyway.\n");
351  return AVERROR(EINVAL);
352  }
353 
354  if (s->bitstream_bpp >= 24 && s->predictor == MEDIAN && s->version <= 2) {
355  av_log(avctx, AV_LOG_ERROR,
356  "Error: RGB is incompatible with median predictor\n");
357  return AVERROR(EINVAL);
358  }
359 
360  ((uint8_t*)avctx->extradata)[0] = s->predictor | (s->decorrelate << 6);
361  ((uint8_t*)avctx->extradata)[2] = s->interlaced ? 0x10 : 0x20;
362  if (s->context)
363  ((uint8_t*)avctx->extradata)[2] |= 0x40;
364  if (s->version < 3) {
365  ((uint8_t*)avctx->extradata)[1] = s->bitstream_bpp;
366  ((uint8_t*)avctx->extradata)[3] = 0;
367  } else {
368  ((uint8_t*)avctx->extradata)[1] = ((s->bps-1)<<4) | s->chroma_h_shift | (s->chroma_v_shift<<2);
369  if (s->chroma)
370  ((uint8_t*)avctx->extradata)[2] |= s->yuv ? 1 : 2;
371  if (s->alpha)
372  ((uint8_t*)avctx->extradata)[2] |= 4;
373  ((uint8_t*)avctx->extradata)[3] = 1;
374  }
375  s->avctx->extradata_size = 4;
376 
377  if (avctx->stats_in) {
378  char *p = avctx->stats_in;
379 
380  for (i = 0; i < 4; i++)
381  for (j = 0; j < s->vlc_n; j++)
382  s->stats[i][j] = 1;
383 
384  for (;;) {
385  for (i = 0; i < 4; i++) {
386  char *next;
387 
388  for (j = 0; j < s->vlc_n; j++) {
389  s->stats[i][j] += strtol(p, &next, 0);
390  if (next == p) return -1;
391  p = next;
392  }
393  }
394  if (p[0] == 0 || p[1] == 0 || p[2] == 0) break;
395  }
396  } else {
397  for (i = 0; i < 4; i++)
398  for (j = 0; j < s->vlc_n; j++) {
399  int d = FFMIN(j, s->vlc_n - j);
400 
401  s->stats[i][j] = 100000000 / (d + 1);
402  }
403  }
404 
406  if (ret < 0)
407  return ret;
408  s->avctx->extradata_size += ret;
409 
410  if (s->context) {
411  for (i = 0; i < 4; i++) {
412  int pels = s->width * s->height / (i ? 40 : 10);
413  for (j = 0; j < s->vlc_n; j++) {
414  int d = FFMIN(j, s->vlc_n - j);
415  s->stats[i][j] = pels/(d + 1);
416  }
417  }
418  } else {
419  for (i = 0; i < 4; i++)
420  for (j = 0; j < s->vlc_n; j++)
421  s->stats[i][j]= 0;
422  }
423 
424  if (ff_huffyuv_alloc_temp(s)) {
426  return AVERROR(ENOMEM);
427  }
428 
429  s->picture_number=0;
430 
431  return 0;
432 }
434 {
435  int i;
436  const uint8_t *y = s->temp[0] + offset;
437  const uint8_t *u = s->temp[1] + offset / 2;
438  const uint8_t *v = s->temp[2] + offset / 2;
439 
440  if (s->pb.buf_end - s->pb.buf - (put_bits_count(&s->pb) >> 3) < 2 * 4 * count) {
441  av_log(s->avctx, AV_LOG_ERROR, "encoded frame too large\n");
442  return -1;
443  }
444 
445 #define LOAD4\
446  int y0 = y[2 * i];\
447  int y1 = y[2 * i + 1];\
448  int u0 = u[i];\
449  int v0 = v[i];
450 
451  count /= 2;
452 
453  if (s->flags & CODEC_FLAG_PASS1) {
454  for(i = 0; i < count; i++) {
455  LOAD4;
456  s->stats[0][y0]++;
457  s->stats[1][u0]++;
458  s->stats[0][y1]++;
459  s->stats[2][v0]++;
460  }
461  }
463  return 0;
464  if (s->context) {
465  for (i = 0; i < count; i++) {
466  LOAD4;
467  s->stats[0][y0]++;
468  put_bits(&s->pb, s->len[0][y0], s->bits[0][y0]);
469  s->stats[1][u0]++;
470  put_bits(&s->pb, s->len[1][u0], s->bits[1][u0]);
471  s->stats[0][y1]++;
472  put_bits(&s->pb, s->len[0][y1], s->bits[0][y1]);
473  s->stats[2][v0]++;
474  put_bits(&s->pb, s->len[2][v0], s->bits[2][v0]);
475  }
476  } else {
477  for(i = 0; i < count; i++) {
478  LOAD4;
479  put_bits(&s->pb, s->len[0][y0], s->bits[0][y0]);
480  put_bits(&s->pb, s->len[1][u0], s->bits[1][u0]);
481  put_bits(&s->pb, s->len[0][y1], s->bits[0][y1]);
482  put_bits(&s->pb, s->len[2][v0], s->bits[2][v0]);
483  }
484  }
485  return 0;
486 }
487 
488 static int encode_plane_bitstream(HYuvContext *s, int count, int plane)
489 {
490  int i;
491 
492  if (s->pb.buf_end - s->pb.buf - (put_bits_count(&s->pb) >> 3) < count * s->bps / 2) {
493  av_log(s->avctx, AV_LOG_ERROR, "encoded frame too large\n");
494  return -1;
495  }
496 
497 #define LOAD2\
498  int y0 = s->temp[0][2 * i];\
499  int y1 = s->temp[0][2 * i + 1];
500 #define LOAD2_14\
501  int y0 = s->temp16[0][2 * i] & mask;\
502  int y1 = s->temp16[0][2 * i + 1] & mask;
503 #define LOAD2_16\
504  int y0 = s->temp16[0][2 * i];\
505  int y1 = s->temp16[0][2 * i + 1];
506 #define STAT2\
507  s->stats[plane][y0]++;\
508  s->stats[plane][y1]++;
509 #define STAT2_16\
510  s->stats[plane][y0>>2]++;\
511  s->stats[plane][y1>>2]++;
512 #define WRITE2\
513  put_bits(&s->pb, s->len[plane][y0], s->bits[plane][y0]);\
514  put_bits(&s->pb, s->len[plane][y1], s->bits[plane][y1]);
515 #define WRITE2_16\
516  put_bits(&s->pb, s->len[plane][y0>>2], s->bits[plane][y0>>2]);\
517  put_bits(&s->pb, 2, y0&3);\
518  put_bits(&s->pb, s->len[plane][y1>>2], s->bits[plane][y1>>2]);\
519  put_bits(&s->pb, 2, y1&3);
520 
521  count /= 2;
522 
523  if (s->bps <= 8) {
524  if (s->flags & CODEC_FLAG_PASS1) {
525  for (i = 0; i < count; i++) {
526  LOAD2;
527  STAT2;
528  }
529  }
531  return 0;
532 
533  if (s->context) {
534  for (i = 0; i < count; i++) {
535  LOAD2;
536  STAT2;
537  WRITE2;
538  }
539  } else {
540  for (i = 0; i < count; i++) {
541  LOAD2;
542  WRITE2;
543  }
544  }
545  } else if (s->bps <= 14) {
546  int mask = s->n - 1;
547  if (s->flags & CODEC_FLAG_PASS1) {
548  for (i = 0; i < count; i++) {
549  LOAD2_14;
550  STAT2;
551  }
552  }
554  return 0;
555 
556  if (s->context) {
557  for (i = 0; i < count; i++) {
558  LOAD2_14;
559  STAT2;
560  WRITE2;
561  }
562  } else {
563  for (i = 0; i < count; i++) {
564  LOAD2_14;
565  WRITE2;
566  }
567  }
568  } else {
569  if (s->flags & CODEC_FLAG_PASS1) {
570  for (i = 0; i < count; i++) {
571  LOAD2_16;
572  STAT2_16;
573  }
574  }
576  return 0;
577 
578  if (s->context) {
579  for (i = 0; i < count; i++) {
580  LOAD2_16;
581  STAT2_16;
582  WRITE2_16;
583  }
584  } else {
585  for (i = 0; i < count; i++) {
586  LOAD2_16;
587  WRITE2_16;
588  }
589  }
590  }
591 #undef LOAD2
592 #undef STAT2
593 #undef WRITE2
594  return 0;
595 }
596 
598 {
599  int i;
600 
601  if (s->pb.buf_end - s->pb.buf - (put_bits_count(&s->pb) >> 3) < 4 * count) {
602  av_log(s->avctx, AV_LOG_ERROR, "encoded frame too large\n");
603  return -1;
604  }
605 
606 #define LOAD2\
607  int y0 = s->temp[0][2 * i];\
608  int y1 = s->temp[0][2 * i + 1];
609 #define STAT2\
610  s->stats[0][y0]++;\
611  s->stats[0][y1]++;
612 #define WRITE2\
613  put_bits(&s->pb, s->len[0][y0], s->bits[0][y0]);\
614  put_bits(&s->pb, s->len[0][y1], s->bits[0][y1]);
615 
616  count /= 2;
617 
618  if (s->flags & CODEC_FLAG_PASS1) {
619  for (i = 0; i < count; i++) {
620  LOAD2;
621  STAT2;
622  }
623  }
625  return 0;
626 
627  if (s->context) {
628  for (i = 0; i < count; i++) {
629  LOAD2;
630  STAT2;
631  WRITE2;
632  }
633  } else {
634  for (i = 0; i < count; i++) {
635  LOAD2;
636  WRITE2;
637  }
638  }
639  return 0;
640 }
641 
642 static inline int encode_bgra_bitstream(HYuvContext *s, int count, int planes)
643 {
644  int i;
645 
646  if (s->pb.buf_end - s->pb.buf - (put_bits_count(&s->pb) >> 3) <
647  4 * planes * count) {
648  av_log(s->avctx, AV_LOG_ERROR, "encoded frame too large\n");
649  return -1;
650  }
651 
652 #define LOAD_GBRA \
653  int g = s->temp[0][planes == 3 ? 3 * i + 1 : 4 * i + G]; \
654  int b =(s->temp[0][planes == 3 ? 3 * i + 2 : 4 * i + B] - g) & 0xFF;\
655  int r =(s->temp[0][planes == 3 ? 3 * i + 0 : 4 * i + R] - g) & 0xFF;\
656  int a = s->temp[0][planes * i + A];
657 
658 #define STAT_BGRA \
659  s->stats[0][b]++; \
660  s->stats[1][g]++; \
661  s->stats[2][r]++; \
662  if (planes == 4) \
663  s->stats[2][a]++;
664 
665 #define WRITE_GBRA \
666  put_bits(&s->pb, s->len[1][g], s->bits[1][g]); \
667  put_bits(&s->pb, s->len[0][b], s->bits[0][b]); \
668  put_bits(&s->pb, s->len[2][r], s->bits[2][r]); \
669  if (planes == 4) \
670  put_bits(&s->pb, s->len[2][a], s->bits[2][a]);
671 
672  if ((s->flags & CODEC_FLAG_PASS1) &&
674  for (i = 0; i < count; i++) {
675  LOAD_GBRA;
676  STAT_BGRA;
677  }
678  } else if (s->context || (s->flags & CODEC_FLAG_PASS1)) {
679  for (i = 0; i < count; i++) {
680  LOAD_GBRA;
681  STAT_BGRA;
682  WRITE_GBRA;
683  }
684  } else {
685  for (i = 0; i < count; i++) {
686  LOAD_GBRA;
687  WRITE_GBRA;
688  }
689  }
690  return 0;
691 }
692 
694  const AVFrame *pict, int *got_packet)
695 {
696  HYuvContext *s = avctx->priv_data;
697  const int width = s->width;
698  const int width2 = s->width>>1;
699  const int height = s->height;
700  const int fake_ystride = s->interlaced ? pict->linesize[0]*2 : pict->linesize[0];
701  const int fake_ustride = s->interlaced ? pict->linesize[1]*2 : pict->linesize[1];
702  const int fake_vstride = s->interlaced ? pict->linesize[2]*2 : pict->linesize[2];
703  const AVFrame * const p = pict;
704  int i, j, size = 0, ret;
705 
706  if ((ret = ff_alloc_packet2(avctx, pkt, width * height * 3 * 4 + FF_MIN_BUFFER_SIZE)) < 0)
707  return ret;
708 
709  if (s->context) {
710  size = store_huffman_tables(s, pkt->data);
711  if (size < 0)
712  return size;
713 
714  for (i = 0; i < 4; i++)
715  for (j = 0; j < s->vlc_n; j++)
716  s->stats[i][j] >>= 1;
717  }
718 
719  init_put_bits(&s->pb, pkt->data + size, pkt->size - size);
720 
721  if (avctx->pix_fmt == AV_PIX_FMT_YUV422P ||
722  avctx->pix_fmt == AV_PIX_FMT_YUV420P) {
723  int lefty, leftu, leftv, y, cy;
724 
725  put_bits(&s->pb, 8, leftv = p->data[2][0]);
726  put_bits(&s->pb, 8, lefty = p->data[0][1]);
727  put_bits(&s->pb, 8, leftu = p->data[1][0]);
728  put_bits(&s->pb, 8, p->data[0][0]);
729 
730  lefty = sub_left_prediction(s, s->temp[0], p->data[0], width , 0);
731  leftu = sub_left_prediction(s, s->temp[1], p->data[1], width2, 0);
732  leftv = sub_left_prediction(s, s->temp[2], p->data[2], width2, 0);
733 
734  encode_422_bitstream(s, 2, width-2);
735 
736  if (s->predictor==MEDIAN) {
737  int lefttopy, lefttopu, lefttopv;
738  cy = y = 1;
739  if (s->interlaced) {
740  lefty = sub_left_prediction(s, s->temp[0], p->data[0] + p->linesize[0], width , lefty);
741  leftu = sub_left_prediction(s, s->temp[1], p->data[1] + p->linesize[1], width2, leftu);
742  leftv = sub_left_prediction(s, s->temp[2], p->data[2] + p->linesize[2], width2, leftv);
743 
744  encode_422_bitstream(s, 0, width);
745  y++; cy++;
746  }
747 
748  lefty = sub_left_prediction(s, s->temp[0], p->data[0] + fake_ystride, 4, lefty);
749  leftu = sub_left_prediction(s, s->temp[1], p->data[1] + fake_ustride, 2, leftu);
750  leftv = sub_left_prediction(s, s->temp[2], p->data[2] + fake_vstride, 2, leftv);
751 
752  encode_422_bitstream(s, 0, 4);
753 
754  lefttopy = p->data[0][3];
755  lefttopu = p->data[1][1];
756  lefttopv = p->data[2][1];
757  s->dsp.sub_hfyu_median_prediction(s->temp[0], p->data[0]+4, p->data[0] + fake_ystride + 4, width - 4 , &lefty, &lefttopy);
758  s->dsp.sub_hfyu_median_prediction(s->temp[1], p->data[1]+2, p->data[1] + fake_ustride + 2, width2 - 2, &leftu, &lefttopu);
759  s->dsp.sub_hfyu_median_prediction(s->temp[2], p->data[2]+2, p->data[2] + fake_vstride + 2, width2 - 2, &leftv, &lefttopv);
760  encode_422_bitstream(s, 0, width - 4);
761  y++; cy++;
762 
763  for (; y < height; y++,cy++) {
764  uint8_t *ydst, *udst, *vdst;
765 
766  if (s->bitstream_bpp == 12) {
767  while (2 * cy > y) {
768  ydst = p->data[0] + p->linesize[0] * y;
769  s->dsp.sub_hfyu_median_prediction(s->temp[0], ydst - fake_ystride, ydst, width , &lefty, &lefttopy);
770  encode_gray_bitstream(s, width);
771  y++;
772  }
773  if (y >= height) break;
774  }
775  ydst = p->data[0] + p->linesize[0] * y;
776  udst = p->data[1] + p->linesize[1] * cy;
777  vdst = p->data[2] + p->linesize[2] * cy;
778 
779  s->dsp.sub_hfyu_median_prediction(s->temp[0], ydst - fake_ystride, ydst, width , &lefty, &lefttopy);
780  s->dsp.sub_hfyu_median_prediction(s->temp[1], udst - fake_ustride, udst, width2, &leftu, &lefttopu);
781  s->dsp.sub_hfyu_median_prediction(s->temp[2], vdst - fake_vstride, vdst, width2, &leftv, &lefttopv);
782 
783  encode_422_bitstream(s, 0, width);
784  }
785  } else {
786  for (cy = y = 1; y < height; y++, cy++) {
787  uint8_t *ydst, *udst, *vdst;
788 
789  /* encode a luma only line & y++ */
790  if (s->bitstream_bpp == 12) {
791  ydst = p->data[0] + p->linesize[0] * y;
792 
793  if (s->predictor == PLANE && s->interlaced < y) {
794  s->dsp.diff_bytes(s->temp[1], ydst, ydst - fake_ystride, width);
795 
796  lefty = sub_left_prediction(s, s->temp[0], s->temp[1], width , lefty);
797  } else {
798  lefty = sub_left_prediction(s, s->temp[0], ydst, width , lefty);
799  }
800  encode_gray_bitstream(s, width);
801  y++;
802  if (y >= height) break;
803  }
804 
805  ydst = p->data[0] + p->linesize[0] * y;
806  udst = p->data[1] + p->linesize[1] * cy;
807  vdst = p->data[2] + p->linesize[2] * cy;
808 
809  if (s->predictor == PLANE && s->interlaced < cy) {
810  s->dsp.diff_bytes(s->temp[1], ydst, ydst - fake_ystride, width);
811  s->dsp.diff_bytes(s->temp[2], udst, udst - fake_ustride, width2);
812  s->dsp.diff_bytes(s->temp[2] + width2, vdst, vdst - fake_vstride, width2);
813 
814  lefty = sub_left_prediction(s, s->temp[0], s->temp[1], width , lefty);
815  leftu = sub_left_prediction(s, s->temp[1], s->temp[2], width2, leftu);
816  leftv = sub_left_prediction(s, s->temp[2], s->temp[2] + width2, width2, leftv);
817  } else {
818  lefty = sub_left_prediction(s, s->temp[0], ydst, width , lefty);
819  leftu = sub_left_prediction(s, s->temp[1], udst, width2, leftu);
820  leftv = sub_left_prediction(s, s->temp[2], vdst, width2, leftv);
821  }
822 
823  encode_422_bitstream(s, 0, width);
824  }
825  }
826  } else if(avctx->pix_fmt == AV_PIX_FMT_RGB32) {
827  uint8_t *data = p->data[0] + (height - 1) * p->linesize[0];
828  const int stride = -p->linesize[0];
829  const int fake_stride = -fake_ystride;
830  int y;
831  int leftr, leftg, leftb, lefta;
832 
833  put_bits(&s->pb, 8, lefta = data[A]);
834  put_bits(&s->pb, 8, leftr = data[R]);
835  put_bits(&s->pb, 8, leftg = data[G]);
836  put_bits(&s->pb, 8, leftb = data[B]);
837 
838  sub_left_prediction_bgr32(s, s->temp[0], data + 4, width - 1,
839  &leftr, &leftg, &leftb, &lefta);
840  encode_bgra_bitstream(s, width - 1, 4);
841 
842  for (y = 1; y < s->height; y++) {
843  uint8_t *dst = data + y*stride;
844  if (s->predictor == PLANE && s->interlaced < y) {
845  s->dsp.diff_bytes(s->temp[1], dst, dst - fake_stride, width * 4);
846  sub_left_prediction_bgr32(s, s->temp[0], s->temp[1], width,
847  &leftr, &leftg, &leftb, &lefta);
848  } else {
849  sub_left_prediction_bgr32(s, s->temp[0], dst, width,
850  &leftr, &leftg, &leftb, &lefta);
851  }
852  encode_bgra_bitstream(s, width, 4);
853  }
854  } else if (avctx->pix_fmt == AV_PIX_FMT_RGB24) {
855  uint8_t *data = p->data[0] + (height - 1) * p->linesize[0];
856  const int stride = -p->linesize[0];
857  const int fake_stride = -fake_ystride;
858  int y;
859  int leftr, leftg, leftb;
860 
861  put_bits(&s->pb, 8, leftr = data[0]);
862  put_bits(&s->pb, 8, leftg = data[1]);
863  put_bits(&s->pb, 8, leftb = data[2]);
864  put_bits(&s->pb, 8, 0);
865 
866  sub_left_prediction_rgb24(s, s->temp[0], data + 3, width - 1,
867  &leftr, &leftg, &leftb);
868  encode_bgra_bitstream(s, width-1, 3);
869 
870  for (y = 1; y < s->height; y++) {
871  uint8_t *dst = data + y * stride;
872  if (s->predictor == PLANE && s->interlaced < y) {
873  s->dsp.diff_bytes(s->temp[1], dst, dst - fake_stride,
874  width * 3);
875  sub_left_prediction_rgb24(s, s->temp[0], s->temp[1], width,
876  &leftr, &leftg, &leftb);
877  } else {
878  sub_left_prediction_rgb24(s, s->temp[0], dst, width,
879  &leftr, &leftg, &leftb);
880  }
881  encode_bgra_bitstream(s, width, 3);
882  }
883  } else if (s->version > 2) {
884  int plane;
885  for (plane = 0; plane < 1 + 2*s->chroma + s->alpha; plane++) {
886  int left, y;
887  int w = width;
888  int h = height;
889  int fake_stride = fake_ystride;
890 
891  if (s->chroma && (plane == 1 || plane == 2)) {
892  w >>= s->chroma_h_shift;
893  h >>= s->chroma_v_shift;
894  fake_stride = plane == 1 ? fake_ustride : fake_vstride;
895  }
896 
897  left = sub_left_prediction(s, s->temp[0], p->data[plane], w , 0);
898 
899  encode_plane_bitstream(s, w, plane);
900 
901  if (s->predictor==MEDIAN) {
902  int lefttop;
903  y = 1;
904  if (s->interlaced) {
905  left = sub_left_prediction(s, s->temp[0], p->data[plane] + p->linesize[plane], w , left);
906 
907  encode_plane_bitstream(s, w, plane);
908  y++;
909  }
910 
911  lefttop = p->data[plane][0];
912 
913  for (; y < h; y++) {
914  uint8_t *dst = p->data[plane] + p->linesize[plane] * y;
915 
916  sub_median_prediction(s, s->temp[0], dst - fake_stride, dst, w , &left, &lefttop);
917 
918  encode_plane_bitstream(s, w, plane);
919  }
920  } else {
921  for (y = 1; y < h; y++) {
922  uint8_t *dst = p->data[plane] + p->linesize[plane] * y;
923 
924  if (s->predictor == PLANE && s->interlaced < y) {
925  diff_bytes(s, s->temp[1], dst, dst - fake_stride, w);
926 
927  left = sub_left_prediction(s, s->temp[0], s->temp[1], w , left);
928  } else {
929  left = sub_left_prediction(s, s->temp[0], dst, w , left);
930  }
931 
932  encode_plane_bitstream(s, w, plane);
933  }
934  }
935  }
936  } else {
937  av_log(avctx, AV_LOG_ERROR, "Format not supported!\n");
938  }
939  emms_c();
940 
941  size += (put_bits_count(&s->pb) + 31) / 8;
942  put_bits(&s->pb, 16, 0);
943  put_bits(&s->pb, 15, 0);
944  size /= 4;
945 
946  if ((s->flags&CODEC_FLAG_PASS1) && (s->picture_number & 31) == 0) {
947  int j;
948  char *p = avctx->stats_out;
949  char *end = p + STATS_OUT_SIZE;
950  for (i = 0; i < 4; i++) {
951  for (j = 0; j < s->vlc_n; j++) {
952  snprintf(p, end-p, "%"PRIu64" ", s->stats[i][j]);
953  p += strlen(p);
954  s->stats[i][j]= 0;
955  }
956  snprintf(p, end-p, "\n");
957  p++;
958  if (end <= p)
959  return AVERROR(ENOMEM);
960  }
961  } else if (avctx->stats_out)
962  avctx->stats_out[0] = '\0';
963  if (!(s->avctx->flags2 & CODEC_FLAG2_NO_OUTPUT)) {
964  flush_put_bits(&s->pb);
965  s->dsp.bswap_buf((uint32_t*)pkt->data, (uint32_t*)pkt->data, size);
966  }
967 
968  s->picture_number++;
969 
970  pkt->size = size * 4;
971  pkt->flags |= AV_PKT_FLAG_KEY;
972  *got_packet = 1;
973 
974  return 0;
975 }
976 
978 {
979  HYuvContext *s = avctx->priv_data;
980 
982 
983  av_freep(&avctx->extradata);
984  av_freep(&avctx->stats_out);
985 
986  av_frame_free(&avctx->coded_frame);
987 
988  return 0;
989 }
990 
991 #if CONFIG_HUFFYUV_ENCODER
992 AVCodec ff_huffyuv_encoder = {
993  .name = "huffyuv",
994  .long_name = NULL_IF_CONFIG_SMALL("Huffyuv / HuffYUV"),
995  .type = AVMEDIA_TYPE_VIDEO,
996  .id = AV_CODEC_ID_HUFFYUV,
997  .priv_data_size = sizeof(HYuvContext),
998  .init = encode_init,
999  .encode2 = encode_frame,
1000  .close = encode_end,
1002  .pix_fmts = (const enum AVPixelFormat[]){
1005  },
1006 };
1007 #endif
1008 
1009 #if CONFIG_FFVHUFF_ENCODER
1010 AVCodec ff_ffvhuff_encoder = {
1011  .name = "ffvhuff",
1012  .long_name = NULL_IF_CONFIG_SMALL("Huffyuv FFmpeg variant"),
1013  .type = AVMEDIA_TYPE_VIDEO,
1014  .id = AV_CODEC_ID_FFVHUFF,
1015  .priv_data_size = sizeof(HYuvContext),
1016  .init = encode_init,
1017  .encode2 = encode_frame,
1018  .close = encode_end,
1020  .pix_fmts = (const enum AVPixelFormat[]){
1037  },
1038 };
1039 #endif