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-2003 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 
24 /**
25  * @file
26  * huffyuv encoder
27  */
28 
29 #include "avcodec.h"
30 #include "huffyuv.h"
31 #include "huffman.h"
32 #include "internal.h"
33 #include "put_bits.h"
34 
35 static inline int sub_left_prediction(HYuvContext *s, uint8_t *dst,
36  const uint8_t *src, int w, int left)
37 {
38  int i;
39  if (w < 32) {
40  for (i = 0; i < w; i++) {
41  const int temp = src[i];
42  dst[i] = temp - left;
43  left = temp;
44  }
45  return left;
46  } else {
47  for (i = 0; i < 16; i++) {
48  const int temp = src[i];
49  dst[i] = temp - left;
50  left = temp;
51  }
52  s->dsp.diff_bytes(dst + 16, src + 16, src + 15, w - 16);
53  return src[w-1];
54  }
55 }
56 
57 static inline void sub_left_prediction_bgr32(HYuvContext *s, uint8_t *dst,
58  const uint8_t *src, int w,
59  int *red, int *green, int *blue,
60  int *alpha)
61 {
62  int i;
63  int r, g, b, a;
64  r = *red;
65  g = *green;
66  b = *blue;
67  a = *alpha;
68 
69  for (i = 0; i < FFMIN(w, 4); i++) {
70  const int rt = src[i * 4 + R];
71  const int gt = src[i * 4 + G];
72  const int bt = src[i * 4 + B];
73  const int at = src[i * 4 + A];
74  dst[i * 4 + R] = rt - r;
75  dst[i * 4 + G] = gt - g;
76  dst[i * 4 + B] = bt - b;
77  dst[i * 4 + A] = at - a;
78  r = rt;
79  g = gt;
80  b = bt;
81  a = at;
82  }
83 
84  s->dsp.diff_bytes(dst + 16, src + 16, src + 12, w * 4 - 16);
85 
86  *red = src[(w - 1) * 4 + R];
87  *green = src[(w - 1) * 4 + G];
88  *blue = src[(w - 1) * 4 + B];
89  *alpha = src[(w - 1) * 4 + A];
90 }
91 
92 static inline void sub_left_prediction_rgb24(HYuvContext *s, uint8_t *dst,
93  uint8_t *src, int w,
94  int *red, int *green, int *blue)
95 {
96  int i;
97  int r, g, b;
98  r = *red;
99  g = *green;
100  b = *blue;
101  for (i = 0; i < FFMIN(w, 16); i++) {
102  const int rt = src[i * 3 + 0];
103  const int gt = src[i * 3 + 1];
104  const int bt = src[i * 3 + 2];
105  dst[i * 3 + 0] = rt - r;
106  dst[i * 3 + 1] = gt - g;
107  dst[i * 3 + 2] = bt - b;
108  r = rt;
109  g = gt;
110  b = bt;
111  }
112 
113  s->dsp.diff_bytes(dst + 48, src + 48, src + 48 - 3, w * 3 - 48);
114 
115  *red = src[(w - 1) * 3 + 0];
116  *green = src[(w - 1) * 3 + 1];
117  *blue = src[(w - 1) * 3 + 2];
118 }
119 
121 {
122  int i;
123  int index = 0;
124 
125  for (i = 0; i < 256;) {
126  int val = len[i];
127  int repeat = 0;
128 
129  for (; i < 256 && len[i] == val && repeat < 255; i++)
130  repeat++;
131 
132  av_assert0(val < 32 && val >0 && repeat<256 && repeat>0);
133  if (repeat > 7) {
134  buf[index++] = val;
135  buf[index++] = repeat;
136  } else {
137  buf[index++] = val | (repeat << 5);
138  }
139  }
140 
141  return index;
142 }
143 
145 {
146  HYuvContext *s = avctx->priv_data;
147  int i, j;
148 
149  ff_huffyuv_common_init(avctx);
150 
151  avctx->extradata = av_mallocz(1024*30); // 256*3+4 == 772
152  avctx->stats_out = av_mallocz(1024*30); // 21*256*3(%llu ) + 3(\n) + 1(0) = 16132
153  if (!avctx->extradata || !avctx->stats_out) {
154  av_freep(&avctx->stats_out);
155  return AVERROR(ENOMEM);
156  }
157  s->version = 2;
158 
159  avctx->coded_frame = av_frame_alloc();
160  if (!avctx->coded_frame)
161  return AVERROR(ENOMEM);
162 
164  avctx->coded_frame->key_frame = 1;
165 
166  switch (avctx->pix_fmt) {
167  case AV_PIX_FMT_YUV420P:
168  case AV_PIX_FMT_YUV422P:
169  if (s->width & 1) {
170  av_log(avctx, AV_LOG_ERROR, "Width must be even for this colorspace.\n");
171  return AVERROR(EINVAL);
172  }
173  s->bitstream_bpp = avctx->pix_fmt == AV_PIX_FMT_YUV420P ? 12 : 16;
174  break;
175  case AV_PIX_FMT_RGB32:
176  s->bitstream_bpp = 32;
177  break;
178  case AV_PIX_FMT_RGB24:
179  s->bitstream_bpp = 24;
180  break;
181  default:
182  av_log(avctx, AV_LOG_ERROR, "format not supported\n");
183  return AVERROR(EINVAL);
184  }
186  s->decorrelate = s->bitstream_bpp >= 24;
187  s->predictor = avctx->prediction_method;
188  s->interlaced = avctx->flags&CODEC_FLAG_INTERLACED_ME ? 1 : 0;
189  if (avctx->context_model == 1) {
190  s->context = avctx->context_model;
192  av_log(avctx, AV_LOG_ERROR,
193  "context=1 is not compatible with "
194  "2 pass huffyuv encoding\n");
195  return AVERROR(EINVAL);
196  }
197  }else s->context= 0;
198 
199  if (avctx->codec->id == AV_CODEC_ID_HUFFYUV) {
200  if (avctx->pix_fmt == AV_PIX_FMT_YUV420P) {
201  av_log(avctx, AV_LOG_ERROR,
202  "Error: YV12 is not supported by huffyuv; use "
203  "vcodec=ffvhuff or format=422p\n");
204  return AVERROR(EINVAL);
205  }
206  if (avctx->context_model) {
207  av_log(avctx, AV_LOG_ERROR,
208  "Error: per-frame huffman tables are not supported "
209  "by huffyuv; use vcodec=ffvhuff\n");
210  return AVERROR(EINVAL);
211  }
212  if (s->interlaced != ( s->height > 288 ))
213  av_log(avctx, AV_LOG_INFO,
214  "using huffyuv 2.2.0 or newer interlacing flag\n");
215  }
216 
217  if (s->bitstream_bpp >= 24 && s->predictor == MEDIAN) {
218  av_log(avctx, AV_LOG_ERROR,
219  "Error: RGB is incompatible with median predictor\n");
220  return AVERROR(EINVAL);
221  }
222 
223  ((uint8_t*)avctx->extradata)[0] = s->predictor | (s->decorrelate << 6);
224  ((uint8_t*)avctx->extradata)[1] = s->bitstream_bpp;
225  ((uint8_t*)avctx->extradata)[2] = s->interlaced ? 0x10 : 0x20;
226  if (s->context)
227  ((uint8_t*)avctx->extradata)[2] |= 0x40;
228  ((uint8_t*)avctx->extradata)[3] = 0;
229  s->avctx->extradata_size = 4;
230 
231  if (avctx->stats_in) {
232  char *p = avctx->stats_in;
233 
234  for (i = 0; i < 3; i++)
235  for (j = 0; j < 256; j++)
236  s->stats[i][j] = 1;
237 
238  for (;;) {
239  for (i = 0; i < 3; i++) {
240  char *next;
241 
242  for (j = 0; j < 256; j++) {
243  s->stats[i][j] += strtol(p, &next, 0);
244  if (next == p) return -1;
245  p = next;
246  }
247  }
248  if (p[0] == 0 || p[1] == 0 || p[2] == 0) break;
249  }
250  } else {
251  for (i = 0; i < 3; i++)
252  for (j = 0; j < 256; j++) {
253  int d = FFMIN(j, 256 - j);
254 
255  s->stats[i][j] = 100000000 / (d + 1);
256  }
257  }
258 
259  for (i = 0; i < 3; i++) {
260  ff_huff_gen_len_table(s->len[i], s->stats[i]);
261 
262  if (ff_huffyuv_generate_bits_table(s->bits[i], s->len[i]) < 0) {
263  return -1;
264  }
265 
266  s->avctx->extradata_size +=
267  store_table(s, s->len[i], &((uint8_t*)s->avctx->extradata)[s->avctx->extradata_size]);
268  }
269 
270  if (s->context) {
271  for (i = 0; i < 3; i++) {
272  int pels = s->width * s->height / (i ? 40 : 10);
273  for (j = 0; j < 256; j++) {
274  int d = FFMIN(j, 256 - j);
275  s->stats[i][j] = pels/(d + 1);
276  }
277  }
278  } else {
279  for (i = 0; i < 3; i++)
280  for (j = 0; j < 256; j++)
281  s->stats[i][j]= 0;
282  }
283 
284  if (ff_huffyuv_alloc_temp(s)) {
286  return AVERROR(ENOMEM);
287  }
288 
289  s->picture_number=0;
290 
291  return 0;
292 }
294 {
295  int i;
296  const uint8_t *y = s->temp[0] + offset;
297  const uint8_t *u = s->temp[1] + offset / 2;
298  const uint8_t *v = s->temp[2] + offset / 2;
299 
300  if (s->pb.buf_end - s->pb.buf - (put_bits_count(&s->pb) >> 3) < 2 * 4 * count) {
301  av_log(s->avctx, AV_LOG_ERROR, "encoded frame too large\n");
302  return -1;
303  }
304 
305 #define LOAD4\
306  int y0 = y[2 * i];\
307  int y1 = y[2 * i + 1];\
308  int u0 = u[i];\
309  int v0 = v[i];
310 
311  count /= 2;
312 
313  if (s->flags & CODEC_FLAG_PASS1) {
314  for(i = 0; i < count; i++) {
315  LOAD4;
316  s->stats[0][y0]++;
317  s->stats[1][u0]++;
318  s->stats[0][y1]++;
319  s->stats[2][v0]++;
320  }
321  }
323  return 0;
324  if (s->context) {
325  for (i = 0; i < count; i++) {
326  LOAD4;
327  s->stats[0][y0]++;
328  put_bits(&s->pb, s->len[0][y0], s->bits[0][y0]);
329  s->stats[1][u0]++;
330  put_bits(&s->pb, s->len[1][u0], s->bits[1][u0]);
331  s->stats[0][y1]++;
332  put_bits(&s->pb, s->len[0][y1], s->bits[0][y1]);
333  s->stats[2][v0]++;
334  put_bits(&s->pb, s->len[2][v0], s->bits[2][v0]);
335  }
336  } else {
337  for(i = 0; i < count; i++) {
338  LOAD4;
339  put_bits(&s->pb, s->len[0][y0], s->bits[0][y0]);
340  put_bits(&s->pb, s->len[1][u0], s->bits[1][u0]);
341  put_bits(&s->pb, s->len[0][y1], s->bits[0][y1]);
342  put_bits(&s->pb, s->len[2][v0], s->bits[2][v0]);
343  }
344  }
345  return 0;
346 }
347 
349 {
350  int i;
351 
352  if (s->pb.buf_end - s->pb.buf - (put_bits_count(&s->pb) >> 3) < 4 * count) {
353  av_log(s->avctx, AV_LOG_ERROR, "encoded frame too large\n");
354  return -1;
355  }
356 
357 #define LOAD2\
358  int y0 = s->temp[0][2 * i];\
359  int y1 = s->temp[0][2 * i + 1];
360 #define STAT2\
361  s->stats[0][y0]++;\
362  s->stats[0][y1]++;
363 #define WRITE2\
364  put_bits(&s->pb, s->len[0][y0], s->bits[0][y0]);\
365  put_bits(&s->pb, s->len[0][y1], s->bits[0][y1]);
366 
367  count /= 2;
368 
369  if (s->flags & CODEC_FLAG_PASS1) {
370  for (i = 0; i < count; i++) {
371  LOAD2;
372  STAT2;
373  }
374  }
376  return 0;
377 
378  if (s->context) {
379  for (i = 0; i < count; i++) {
380  LOAD2;
381  STAT2;
382  WRITE2;
383  }
384  } else {
385  for (i = 0; i < count; i++) {
386  LOAD2;
387  WRITE2;
388  }
389  }
390  return 0;
391 }
392 
393 static inline int encode_bgra_bitstream(HYuvContext *s, int count, int planes)
394 {
395  int i;
396 
397  if (s->pb.buf_end - s->pb.buf - (put_bits_count(&s->pb) >> 3) <
398  4 * planes * count) {
399  av_log(s->avctx, AV_LOG_ERROR, "encoded frame too large\n");
400  return -1;
401  }
402 
403 #define LOAD_GBRA \
404  int g = s->temp[0][planes == 3 ? 3 * i + 1 : 4 * i + G]; \
405  int b =(s->temp[0][planes == 3 ? 3 * i + 2 : 4 * i + B] - g) & 0xFF;\
406  int r =(s->temp[0][planes == 3 ? 3 * i + 0 : 4 * i + R] - g) & 0xFF;\
407  int a = s->temp[0][planes * i + A];
408 
409 #define STAT_BGRA \
410  s->stats[0][b]++; \
411  s->stats[1][g]++; \
412  s->stats[2][r]++; \
413  if (planes == 4) \
414  s->stats[2][a]++;
415 
416 #define WRITE_GBRA \
417  put_bits(&s->pb, s->len[1][g], s->bits[1][g]); \
418  put_bits(&s->pb, s->len[0][b], s->bits[0][b]); \
419  put_bits(&s->pb, s->len[2][r], s->bits[2][r]); \
420  if (planes == 4) \
421  put_bits(&s->pb, s->len[2][a], s->bits[2][a]);
422 
423  if ((s->flags & CODEC_FLAG_PASS1) &&
425  for (i = 0; i < count; i++) {
426  LOAD_GBRA;
427  STAT_BGRA;
428  }
429  } else if (s->context || (s->flags & CODEC_FLAG_PASS1)) {
430  for (i = 0; i < count; i++) {
431  LOAD_GBRA;
432  STAT_BGRA;
433  WRITE_GBRA;
434  }
435  } else {
436  for (i = 0; i < count; i++) {
437  LOAD_GBRA;
438  WRITE_GBRA;
439  }
440  }
441  return 0;
442 }
443 
445  const AVFrame *pict, int *got_packet)
446 {
447  HYuvContext *s = avctx->priv_data;
448  const int width = s->width;
449  const int width2 = s->width>>1;
450  const int height = s->height;
451  const int fake_ystride = s->interlaced ? pict->linesize[0]*2 : pict->linesize[0];
452  const int fake_ustride = s->interlaced ? pict->linesize[1]*2 : pict->linesize[1];
453  const int fake_vstride = s->interlaced ? pict->linesize[2]*2 : pict->linesize[2];
454  const AVFrame * const p = pict;
455  int i, j, size = 0, ret;
456 
457  if ((ret = ff_alloc_packet2(avctx, pkt, width * height * 3 * 4 + FF_MIN_BUFFER_SIZE)) < 0)
458  return ret;
459 
460  if (s->context) {
461  for (i = 0; i < 3; i++) {
462  ff_huff_gen_len_table(s->len[i], s->stats[i]);
463  if (ff_huffyuv_generate_bits_table(s->bits[i], s->len[i]) < 0)
464  return -1;
465  size += store_table(s, s->len[i], &pkt->data[size]);
466  }
467 
468  for (i = 0; i < 3; i++)
469  for (j = 0; j < 256; j++)
470  s->stats[i][j] >>= 1;
471  }
472 
473  init_put_bits(&s->pb, pkt->data + size, pkt->size - size);
474 
475  if (avctx->pix_fmt == AV_PIX_FMT_YUV422P ||
476  avctx->pix_fmt == AV_PIX_FMT_YUV420P) {
477  int lefty, leftu, leftv, y, cy;
478 
479  put_bits(&s->pb, 8, leftv = p->data[2][0]);
480  put_bits(&s->pb, 8, lefty = p->data[0][1]);
481  put_bits(&s->pb, 8, leftu = p->data[1][0]);
482  put_bits(&s->pb, 8, p->data[0][0]);
483 
484  lefty = sub_left_prediction(s, s->temp[0], p->data[0], width , 0);
485  leftu = sub_left_prediction(s, s->temp[1], p->data[1], width2, 0);
486  leftv = sub_left_prediction(s, s->temp[2], p->data[2], width2, 0);
487 
488  encode_422_bitstream(s, 2, width-2);
489 
490  if (s->predictor==MEDIAN) {
491  int lefttopy, lefttopu, lefttopv;
492  cy = y = 1;
493  if (s->interlaced) {
494  lefty = sub_left_prediction(s, s->temp[0], p->data[0] + p->linesize[0], width , lefty);
495  leftu = sub_left_prediction(s, s->temp[1], p->data[1] + p->linesize[1], width2, leftu);
496  leftv = sub_left_prediction(s, s->temp[2], p->data[2] + p->linesize[2], width2, leftv);
497 
498  encode_422_bitstream(s, 0, width);
499  y++; cy++;
500  }
501 
502  lefty = sub_left_prediction(s, s->temp[0], p->data[0] + fake_ystride, 4, lefty);
503  leftu = sub_left_prediction(s, s->temp[1], p->data[1] + fake_ustride, 2, leftu);
504  leftv = sub_left_prediction(s, s->temp[2], p->data[2] + fake_vstride, 2, leftv);
505 
506  encode_422_bitstream(s, 0, 4);
507 
508  lefttopy = p->data[0][3];
509  lefttopu = p->data[1][1];
510  lefttopv = p->data[2][1];
511  s->dsp.sub_hfyu_median_prediction(s->temp[0], p->data[0]+4, p->data[0] + fake_ystride + 4, width - 4 , &lefty, &lefttopy);
512  s->dsp.sub_hfyu_median_prediction(s->temp[1], p->data[1]+2, p->data[1] + fake_ustride + 2, width2 - 2, &leftu, &lefttopu);
513  s->dsp.sub_hfyu_median_prediction(s->temp[2], p->data[2]+2, p->data[2] + fake_vstride + 2, width2 - 2, &leftv, &lefttopv);
514  encode_422_bitstream(s, 0, width - 4);
515  y++; cy++;
516 
517  for (; y < height; y++,cy++) {
518  uint8_t *ydst, *udst, *vdst;
519 
520  if (s->bitstream_bpp == 12) {
521  while (2 * cy > y) {
522  ydst = p->data[0] + p->linesize[0] * y;
523  s->dsp.sub_hfyu_median_prediction(s->temp[0], ydst - fake_ystride, ydst, width , &lefty, &lefttopy);
524  encode_gray_bitstream(s, width);
525  y++;
526  }
527  if (y >= height) break;
528  }
529  ydst = p->data[0] + p->linesize[0] * y;
530  udst = p->data[1] + p->linesize[1] * cy;
531  vdst = p->data[2] + p->linesize[2] * cy;
532 
533  s->dsp.sub_hfyu_median_prediction(s->temp[0], ydst - fake_ystride, ydst, width , &lefty, &lefttopy);
534  s->dsp.sub_hfyu_median_prediction(s->temp[1], udst - fake_ustride, udst, width2, &leftu, &lefttopu);
535  s->dsp.sub_hfyu_median_prediction(s->temp[2], vdst - fake_vstride, vdst, width2, &leftv, &lefttopv);
536 
537  encode_422_bitstream(s, 0, width);
538  }
539  } else {
540  for (cy = y = 1; y < height; y++, cy++) {
541  uint8_t *ydst, *udst, *vdst;
542 
543  /* encode a luma only line & y++ */
544  if (s->bitstream_bpp == 12) {
545  ydst = p->data[0] + p->linesize[0] * y;
546 
547  if (s->predictor == PLANE && s->interlaced < y) {
548  s->dsp.diff_bytes(s->temp[1], ydst, ydst - fake_ystride, width);
549 
550  lefty = sub_left_prediction(s, s->temp[0], s->temp[1], width , lefty);
551  } else {
552  lefty = sub_left_prediction(s, s->temp[0], ydst, width , lefty);
553  }
554  encode_gray_bitstream(s, width);
555  y++;
556  if (y >= height) break;
557  }
558 
559  ydst = p->data[0] + p->linesize[0] * y;
560  udst = p->data[1] + p->linesize[1] * cy;
561  vdst = p->data[2] + p->linesize[2] * cy;
562 
563  if (s->predictor == PLANE && s->interlaced < cy) {
564  s->dsp.diff_bytes(s->temp[1], ydst, ydst - fake_ystride, width);
565  s->dsp.diff_bytes(s->temp[2], udst, udst - fake_ustride, width2);
566  s->dsp.diff_bytes(s->temp[2] + width2, vdst, vdst - fake_vstride, width2);
567 
568  lefty = sub_left_prediction(s, s->temp[0], s->temp[1], width , lefty);
569  leftu = sub_left_prediction(s, s->temp[1], s->temp[2], width2, leftu);
570  leftv = sub_left_prediction(s, s->temp[2], s->temp[2] + width2, width2, leftv);
571  } else {
572  lefty = sub_left_prediction(s, s->temp[0], ydst, width , lefty);
573  leftu = sub_left_prediction(s, s->temp[1], udst, width2, leftu);
574  leftv = sub_left_prediction(s, s->temp[2], vdst, width2, leftv);
575  }
576 
577  encode_422_bitstream(s, 0, width);
578  }
579  }
580  } else if(avctx->pix_fmt == AV_PIX_FMT_RGB32) {
581  uint8_t *data = p->data[0] + (height - 1) * p->linesize[0];
582  const int stride = -p->linesize[0];
583  const int fake_stride = -fake_ystride;
584  int y;
585  int leftr, leftg, leftb, lefta;
586 
587  put_bits(&s->pb, 8, lefta = data[A]);
588  put_bits(&s->pb, 8, leftr = data[R]);
589  put_bits(&s->pb, 8, leftg = data[G]);
590  put_bits(&s->pb, 8, leftb = data[B]);
591 
592  sub_left_prediction_bgr32(s, s->temp[0], data + 4, width - 1,
593  &leftr, &leftg, &leftb, &lefta);
594  encode_bgra_bitstream(s, width - 1, 4);
595 
596  for (y = 1; y < s->height; y++) {
597  uint8_t *dst = data + y*stride;
598  if (s->predictor == PLANE && s->interlaced < y) {
599  s->dsp.diff_bytes(s->temp[1], dst, dst - fake_stride, width * 4);
600  sub_left_prediction_bgr32(s, s->temp[0], s->temp[1], width,
601  &leftr, &leftg, &leftb, &lefta);
602  } else {
603  sub_left_prediction_bgr32(s, s->temp[0], dst, width,
604  &leftr, &leftg, &leftb, &lefta);
605  }
606  encode_bgra_bitstream(s, width, 4);
607  }
608  } else if (avctx->pix_fmt == AV_PIX_FMT_RGB24) {
609  uint8_t *data = p->data[0] + (height - 1) * p->linesize[0];
610  const int stride = -p->linesize[0];
611  const int fake_stride = -fake_ystride;
612  int y;
613  int leftr, leftg, leftb;
614 
615  put_bits(&s->pb, 8, leftr = data[0]);
616  put_bits(&s->pb, 8, leftg = data[1]);
617  put_bits(&s->pb, 8, leftb = data[2]);
618  put_bits(&s->pb, 8, 0);
619 
620  sub_left_prediction_rgb24(s, s->temp[0], data + 3, width - 1,
621  &leftr, &leftg, &leftb);
622  encode_bgra_bitstream(s, width-1, 3);
623 
624  for (y = 1; y < s->height; y++) {
625  uint8_t *dst = data + y * stride;
626  if (s->predictor == PLANE && s->interlaced < y) {
627  s->dsp.diff_bytes(s->temp[1], dst, dst - fake_stride,
628  width * 3);
629  sub_left_prediction_rgb24(s, s->temp[0], s->temp[1], width,
630  &leftr, &leftg, &leftb);
631  } else {
632  sub_left_prediction_rgb24(s, s->temp[0], dst, width,
633  &leftr, &leftg, &leftb);
634  }
635  encode_bgra_bitstream(s, width, 3);
636  }
637  } else {
638  av_log(avctx, AV_LOG_ERROR, "Format not supported!\n");
639  }
640  emms_c();
641 
642  size += (put_bits_count(&s->pb) + 31) / 8;
643  put_bits(&s->pb, 16, 0);
644  put_bits(&s->pb, 15, 0);
645  size /= 4;
646 
647  if ((s->flags&CODEC_FLAG_PASS1) && (s->picture_number & 31) == 0) {
648  int j;
649  char *p = avctx->stats_out;
650  char *end = p + 1024*30;
651  for (i = 0; i < 3; i++) {
652  for (j = 0; j < 256; j++) {
653  snprintf(p, end-p, "%"PRIu64" ", s->stats[i][j]);
654  p += strlen(p);
655  s->stats[i][j]= 0;
656  }
657  snprintf(p, end-p, "\n");
658  p++;
659  }
660  } else
661  avctx->stats_out[0] = '\0';
662  if (!(s->avctx->flags2 & CODEC_FLAG2_NO_OUTPUT)) {
663  flush_put_bits(&s->pb);
664  s->dsp.bswap_buf((uint32_t*)pkt->data, (uint32_t*)pkt->data, size);
665  }
666 
667  s->picture_number++;
668 
669  pkt->size = size * 4;
670  pkt->flags |= AV_PKT_FLAG_KEY;
671  *got_packet = 1;
672 
673  return 0;
674 }
675 
677 {
678  HYuvContext *s = avctx->priv_data;
679 
681 
682  av_freep(&avctx->extradata);
683  av_freep(&avctx->stats_out);
684 
685  av_frame_free(&avctx->coded_frame);
686 
687  return 0;
688 }
689 
690 #if CONFIG_HUFFYUV_ENCODER
691 AVCodec ff_huffyuv_encoder = {
692  .name = "huffyuv",
693  .long_name = NULL_IF_CONFIG_SMALL("Huffyuv / HuffYUV"),
694  .type = AVMEDIA_TYPE_VIDEO,
695  .id = AV_CODEC_ID_HUFFYUV,
696  .priv_data_size = sizeof(HYuvContext),
697  .init = encode_init,
698  .encode2 = encode_frame,
699  .close = encode_end,
700  .pix_fmts = (const enum AVPixelFormat[]){
703  },
704 };
705 #endif
706 
707 #if CONFIG_FFVHUFF_ENCODER
708 AVCodec ff_ffvhuff_encoder = {
709  .name = "ffvhuff",
710  .long_name = NULL_IF_CONFIG_SMALL("Huffyuv FFmpeg variant"),
711  .type = AVMEDIA_TYPE_VIDEO,
712  .id = AV_CODEC_ID_FFVHUFF,
713  .priv_data_size = sizeof(HYuvContext),
714  .init = encode_init,
715  .encode2 = encode_frame,
716  .close = encode_end,
717  .pix_fmts = (const enum AVPixelFormat[]){
720  },
721 };
722 #endif