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