FFmpeg  2.1.1
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 = &s->picture;
160 
161  switch (avctx->pix_fmt) {
162  case AV_PIX_FMT_YUV420P:
163  case AV_PIX_FMT_YUV422P:
164  if (s->width & 1) {
165  av_log(avctx, AV_LOG_ERROR, "Width must be even for this colorspace.\n");
166  return AVERROR(EINVAL);
167  }
168  s->bitstream_bpp = avctx->pix_fmt == AV_PIX_FMT_YUV420P ? 12 : 16;
169  break;
170  case AV_PIX_FMT_RGB32:
171  s->bitstream_bpp = 32;
172  break;
173  case AV_PIX_FMT_RGB24:
174  s->bitstream_bpp = 24;
175  break;
176  default:
177  av_log(avctx, AV_LOG_ERROR, "format not supported\n");
178  return AVERROR(EINVAL);
179  }
181  s->decorrelate = s->bitstream_bpp >= 24;
182  s->predictor = avctx->prediction_method;
183  s->interlaced = avctx->flags&CODEC_FLAG_INTERLACED_ME ? 1 : 0;
184  if (avctx->context_model == 1) {
185  s->context = avctx->context_model;
187  av_log(avctx, AV_LOG_ERROR,
188  "context=1 is not compatible with "
189  "2 pass huffyuv encoding\n");
190  return AVERROR(EINVAL);
191  }
192  }else s->context= 0;
193 
194  if (avctx->codec->id == AV_CODEC_ID_HUFFYUV) {
195  if (avctx->pix_fmt == AV_PIX_FMT_YUV420P) {
196  av_log(avctx, AV_LOG_ERROR,
197  "Error: YV12 is not supported by huffyuv; use "
198  "vcodec=ffvhuff or format=422p\n");
199  return AVERROR(EINVAL);
200  }
201  if (avctx->context_model) {
202  av_log(avctx, AV_LOG_ERROR,
203  "Error: per-frame huffman tables are not supported "
204  "by huffyuv; use vcodec=ffvhuff\n");
205  return AVERROR(EINVAL);
206  }
207  if (s->interlaced != ( s->height > 288 ))
208  av_log(avctx, AV_LOG_INFO,
209  "using huffyuv 2.2.0 or newer interlacing flag\n");
210  }
211 
212  if (s->bitstream_bpp >= 24 && s->predictor == MEDIAN) {
213  av_log(avctx, AV_LOG_ERROR,
214  "Error: RGB is incompatible with median predictor\n");
215  return AVERROR(EINVAL);
216  }
217 
218  ((uint8_t*)avctx->extradata)[0] = s->predictor | (s->decorrelate << 6);
219  ((uint8_t*)avctx->extradata)[1] = s->bitstream_bpp;
220  ((uint8_t*)avctx->extradata)[2] = s->interlaced ? 0x10 : 0x20;
221  if (s->context)
222  ((uint8_t*)avctx->extradata)[2] |= 0x40;
223  ((uint8_t*)avctx->extradata)[3] = 0;
224  s->avctx->extradata_size = 4;
225 
226  if (avctx->stats_in) {
227  char *p = avctx->stats_in;
228 
229  for (i = 0; i < 3; i++)
230  for (j = 0; j < 256; j++)
231  s->stats[i][j] = 1;
232 
233  for (;;) {
234  for (i = 0; i < 3; i++) {
235  char *next;
236 
237  for (j = 0; j < 256; j++) {
238  s->stats[i][j] += strtol(p, &next, 0);
239  if (next == p) return -1;
240  p = next;
241  }
242  }
243  if (p[0] == 0 || p[1] == 0 || p[2] == 0) break;
244  }
245  } else {
246  for (i = 0; i < 3; i++)
247  for (j = 0; j < 256; j++) {
248  int d = FFMIN(j, 256 - j);
249 
250  s->stats[i][j] = 100000000 / (d + 1);
251  }
252  }
253 
254  for (i = 0; i < 3; i++) {
255  ff_huff_gen_len_table(s->len[i], s->stats[i]);
256 
257  if (ff_huffyuv_generate_bits_table(s->bits[i], s->len[i]) < 0) {
258  return -1;
259  }
260 
261  s->avctx->extradata_size +=
262  store_table(s, s->len[i], &((uint8_t*)s->avctx->extradata)[s->avctx->extradata_size]);
263  }
264 
265  if (s->context) {
266  for (i = 0; i < 3; i++) {
267  int pels = s->width * s->height / (i ? 40 : 10);
268  for (j = 0; j < 256; j++) {
269  int d = FFMIN(j, 256 - j);
270  s->stats[i][j] = pels/(d + 1);
271  }
272  }
273  } else {
274  for (i = 0; i < 3; i++)
275  for (j = 0; j < 256; j++)
276  s->stats[i][j]= 0;
277  }
278 
279  if (ff_huffyuv_alloc_temp(s)) {
281  return AVERROR(ENOMEM);
282  }
283 
284  s->picture_number=0;
285 
286  return 0;
287 }
289 {
290  int i;
291  const uint8_t *y = s->temp[0] + offset;
292  const uint8_t *u = s->temp[1] + offset / 2;
293  const uint8_t *v = s->temp[2] + offset / 2;
294 
295  if (s->pb.buf_end - s->pb.buf - (put_bits_count(&s->pb) >> 3) < 2 * 4 * count) {
296  av_log(s->avctx, AV_LOG_ERROR, "encoded frame too large\n");
297  return -1;
298  }
299 
300 #define LOAD4\
301  int y0 = y[2 * i];\
302  int y1 = y[2 * i + 1];\
303  int u0 = u[i];\
304  int v0 = v[i];
305 
306  count /= 2;
307 
308  if (s->flags & CODEC_FLAG_PASS1) {
309  for(i = 0; i < count; i++) {
310  LOAD4;
311  s->stats[0][y0]++;
312  s->stats[1][u0]++;
313  s->stats[0][y1]++;
314  s->stats[2][v0]++;
315  }
316  }
318  return 0;
319  if (s->context) {
320  for (i = 0; i < count; i++) {
321  LOAD4;
322  s->stats[0][y0]++;
323  put_bits(&s->pb, s->len[0][y0], s->bits[0][y0]);
324  s->stats[1][u0]++;
325  put_bits(&s->pb, s->len[1][u0], s->bits[1][u0]);
326  s->stats[0][y1]++;
327  put_bits(&s->pb, s->len[0][y1], s->bits[0][y1]);
328  s->stats[2][v0]++;
329  put_bits(&s->pb, s->len[2][v0], s->bits[2][v0]);
330  }
331  } else {
332  for(i = 0; i < count; i++) {
333  LOAD4;
334  put_bits(&s->pb, s->len[0][y0], s->bits[0][y0]);
335  put_bits(&s->pb, s->len[1][u0], s->bits[1][u0]);
336  put_bits(&s->pb, s->len[0][y1], s->bits[0][y1]);
337  put_bits(&s->pb, s->len[2][v0], s->bits[2][v0]);
338  }
339  }
340  return 0;
341 }
342 
344 {
345  int i;
346 
347  if (s->pb.buf_end - s->pb.buf - (put_bits_count(&s->pb) >> 3) < 4 * count) {
348  av_log(s->avctx, AV_LOG_ERROR, "encoded frame too large\n");
349  return -1;
350  }
351 
352 #define LOAD2\
353  int y0 = s->temp[0][2 * i];\
354  int y1 = s->temp[0][2 * i + 1];
355 #define STAT2\
356  s->stats[0][y0]++;\
357  s->stats[0][y1]++;
358 #define WRITE2\
359  put_bits(&s->pb, s->len[0][y0], s->bits[0][y0]);\
360  put_bits(&s->pb, s->len[0][y1], s->bits[0][y1]);
361 
362  count /= 2;
363 
364  if (s->flags & CODEC_FLAG_PASS1) {
365  for (i = 0; i < count; i++) {
366  LOAD2;
367  STAT2;
368  }
369  }
371  return 0;
372 
373  if (s->context) {
374  for (i = 0; i < count; i++) {
375  LOAD2;
376  STAT2;
377  WRITE2;
378  }
379  } else {
380  for (i = 0; i < count; i++) {
381  LOAD2;
382  WRITE2;
383  }
384  }
385  return 0;
386 }
387 
388 static inline int encode_bgra_bitstream(HYuvContext *s, int count, int planes)
389 {
390  int i;
391 
392  if (s->pb.buf_end - s->pb.buf - (put_bits_count(&s->pb) >> 3) <
393  4 * planes * count) {
394  av_log(s->avctx, AV_LOG_ERROR, "encoded frame too large\n");
395  return -1;
396  }
397 
398 #define LOAD_GBRA \
399  int g = s->temp[0][planes == 3 ? 3 * i + 1 : 4 * i + G]; \
400  int b =(s->temp[0][planes == 3 ? 3 * i + 2 : 4 * i + B] - g) & 0xFF;\
401  int r =(s->temp[0][planes == 3 ? 3 * i + 0 : 4 * i + R] - g) & 0xFF;\
402  int a = s->temp[0][planes * i + A];
403 
404 #define STAT_BGRA \
405  s->stats[0][b]++; \
406  s->stats[1][g]++; \
407  s->stats[2][r]++; \
408  if (planes == 4) \
409  s->stats[2][a]++;
410 
411 #define WRITE_GBRA \
412  put_bits(&s->pb, s->len[1][g], s->bits[1][g]); \
413  put_bits(&s->pb, s->len[0][b], s->bits[0][b]); \
414  put_bits(&s->pb, s->len[2][r], s->bits[2][r]); \
415  if (planes == 4) \
416  put_bits(&s->pb, s->len[2][a], s->bits[2][a]);
417 
418  if ((s->flags & CODEC_FLAG_PASS1) &&
420  for (i = 0; i < count; i++) {
421  LOAD_GBRA;
422  STAT_BGRA;
423  }
424  } else if (s->context || (s->flags & CODEC_FLAG_PASS1)) {
425  for (i = 0; i < count; i++) {
426  LOAD_GBRA;
427  STAT_BGRA;
428  WRITE_GBRA;
429  }
430  } else {
431  for (i = 0; i < count; i++) {
432  LOAD_GBRA;
433  WRITE_GBRA;
434  }
435  }
436  return 0;
437 }
438 
440  const AVFrame *pict, int *got_packet)
441 {
442  HYuvContext *s = avctx->priv_data;
443  const int width = s->width;
444  const int width2 = s->width>>1;
445  const int height = s->height;
446  const int fake_ystride = s->interlaced ? pict->linesize[0]*2 : pict->linesize[0];
447  const int fake_ustride = s->interlaced ? pict->linesize[1]*2 : pict->linesize[1];
448  const int fake_vstride = s->interlaced ? pict->linesize[2]*2 : pict->linesize[2];
449  AVFrame * const p = &s->picture;
450  int i, j, size = 0, ret;
451 
452  if ((ret = ff_alloc_packet2(avctx, pkt, width * height * 3 * 4 + FF_MIN_BUFFER_SIZE)) < 0)
453  return ret;
454 
455  *p = *pict;
457  p->key_frame = 1;
458 
459  if (s->context) {
460  for (i = 0; i < 3; i++) {
461  ff_huff_gen_len_table(s->len[i], s->stats[i]);
462  if (ff_huffyuv_generate_bits_table(s->bits[i], s->len[i]) < 0)
463  return -1;
464  size += store_table(s, s->len[i], &pkt->data[size]);
465  }
466 
467  for (i = 0; i < 3; i++)
468  for (j = 0; j < 256; j++)
469  s->stats[i][j] >>= 1;
470  }
471 
472  init_put_bits(&s->pb, pkt->data + size, pkt->size - size);
473 
474  if (avctx->pix_fmt == AV_PIX_FMT_YUV422P ||
475  avctx->pix_fmt == AV_PIX_FMT_YUV420P) {
476  int lefty, leftu, leftv, y, cy;
477 
478  put_bits(&s->pb, 8, leftv = p->data[2][0]);
479  put_bits(&s->pb, 8, lefty = p->data[0][1]);
480  put_bits(&s->pb, 8, leftu = p->data[1][0]);
481  put_bits(&s->pb, 8, p->data[0][0]);
482 
483  lefty = sub_left_prediction(s, s->temp[0], p->data[0], width , 0);
484  leftu = sub_left_prediction(s, s->temp[1], p->data[1], width2, 0);
485  leftv = sub_left_prediction(s, s->temp[2], p->data[2], width2, 0);
486 
487  encode_422_bitstream(s, 2, width-2);
488 
489  if (s->predictor==MEDIAN) {
490  int lefttopy, lefttopu, lefttopv;
491  cy = y = 1;
492  if (s->interlaced) {
493  lefty = sub_left_prediction(s, s->temp[0], p->data[0] + p->linesize[0], width , lefty);
494  leftu = sub_left_prediction(s, s->temp[1], p->data[1] + p->linesize[1], width2, leftu);
495  leftv = sub_left_prediction(s, s->temp[2], p->data[2] + p->linesize[2], width2, leftv);
496 
497  encode_422_bitstream(s, 0, width);
498  y++; cy++;
499  }
500 
501  lefty = sub_left_prediction(s, s->temp[0], p->data[0] + fake_ystride, 4, lefty);
502  leftu = sub_left_prediction(s, s->temp[1], p->data[1] + fake_ustride, 2, leftu);
503  leftv = sub_left_prediction(s, s->temp[2], p->data[2] + fake_vstride, 2, leftv);
504 
505  encode_422_bitstream(s, 0, 4);
506 
507  lefttopy = p->data[0][3];
508  lefttopu = p->data[1][1];
509  lefttopv = p->data[2][1];
510  s->dsp.sub_hfyu_median_prediction(s->temp[0], p->data[0]+4, p->data[0] + fake_ystride + 4, width - 4 , &lefty, &lefttopy);
511  s->dsp.sub_hfyu_median_prediction(s->temp[1], p->data[1]+2, p->data[1] + fake_ustride + 2, width2 - 2, &leftu, &lefttopu);
512  s->dsp.sub_hfyu_median_prediction(s->temp[2], p->data[2]+2, p->data[2] + fake_vstride + 2, width2 - 2, &leftv, &lefttopv);
513  encode_422_bitstream(s, 0, width - 4);
514  y++; cy++;
515 
516  for (; y < height; y++,cy++) {
517  uint8_t *ydst, *udst, *vdst;
518 
519  if (s->bitstream_bpp == 12) {
520  while (2 * cy > y) {
521  ydst = p->data[0] + p->linesize[0] * y;
522  s->dsp.sub_hfyu_median_prediction(s->temp[0], ydst - fake_ystride, ydst, width , &lefty, &lefttopy);
523  encode_gray_bitstream(s, width);
524  y++;
525  }
526  if (y >= height) break;
527  }
528  ydst = p->data[0] + p->linesize[0] * y;
529  udst = p->data[1] + p->linesize[1] * cy;
530  vdst = p->data[2] + p->linesize[2] * cy;
531 
532  s->dsp.sub_hfyu_median_prediction(s->temp[0], ydst - fake_ystride, ydst, width , &lefty, &lefttopy);
533  s->dsp.sub_hfyu_median_prediction(s->temp[1], udst - fake_ustride, udst, width2, &leftu, &lefttopu);
534  s->dsp.sub_hfyu_median_prediction(s->temp[2], vdst - fake_vstride, vdst, width2, &leftv, &lefttopv);
535 
536  encode_422_bitstream(s, 0, width);
537  }
538  } else {
539  for (cy = y = 1; y < height; y++, cy++) {
540  uint8_t *ydst, *udst, *vdst;
541 
542  /* encode a luma only line & y++ */
543  if (s->bitstream_bpp == 12) {
544  ydst = p->data[0] + p->linesize[0] * y;
545 
546  if (s->predictor == PLANE && s->interlaced < y) {
547  s->dsp.diff_bytes(s->temp[1], ydst, ydst - fake_ystride, width);
548 
549  lefty = sub_left_prediction(s, s->temp[0], s->temp[1], width , lefty);
550  } else {
551  lefty = sub_left_prediction(s, s->temp[0], ydst, width , lefty);
552  }
553  encode_gray_bitstream(s, width);
554  y++;
555  if (y >= height) break;
556  }
557 
558  ydst = p->data[0] + p->linesize[0] * y;
559  udst = p->data[1] + p->linesize[1] * cy;
560  vdst = p->data[2] + p->linesize[2] * cy;
561 
562  if (s->predictor == PLANE && s->interlaced < cy) {
563  s->dsp.diff_bytes(s->temp[1], ydst, ydst - fake_ystride, width);
564  s->dsp.diff_bytes(s->temp[2], udst, udst - fake_ustride, width2);
565  s->dsp.diff_bytes(s->temp[2] + width2, vdst, vdst - fake_vstride, width2);
566 
567  lefty = sub_left_prediction(s, s->temp[0], s->temp[1], width , lefty);
568  leftu = sub_left_prediction(s, s->temp[1], s->temp[2], width2, leftu);
569  leftv = sub_left_prediction(s, s->temp[2], s->temp[2] + width2, width2, leftv);
570  } else {
571  lefty = sub_left_prediction(s, s->temp[0], ydst, width , lefty);
572  leftu = sub_left_prediction(s, s->temp[1], udst, width2, leftu);
573  leftv = sub_left_prediction(s, s->temp[2], vdst, width2, leftv);
574  }
575 
576  encode_422_bitstream(s, 0, width);
577  }
578  }
579  } else if(avctx->pix_fmt == AV_PIX_FMT_RGB32) {
580  uint8_t *data = p->data[0] + (height - 1) * p->linesize[0];
581  const int stride = -p->linesize[0];
582  const int fake_stride = -fake_ystride;
583  int y;
584  int leftr, leftg, leftb, lefta;
585 
586  put_bits(&s->pb, 8, lefta = data[A]);
587  put_bits(&s->pb, 8, leftr = data[R]);
588  put_bits(&s->pb, 8, leftg = data[G]);
589  put_bits(&s->pb, 8, leftb = data[B]);
590 
591  sub_left_prediction_bgr32(s, s->temp[0], data + 4, width - 1,
592  &leftr, &leftg, &leftb, &lefta);
593  encode_bgra_bitstream(s, width - 1, 4);
594 
595  for (y = 1; y < s->height; y++) {
596  uint8_t *dst = data + y*stride;
597  if (s->predictor == PLANE && s->interlaced < y) {
598  s->dsp.diff_bytes(s->temp[1], dst, dst - fake_stride, width * 4);
599  sub_left_prediction_bgr32(s, s->temp[0], s->temp[1], width,
600  &leftr, &leftg, &leftb, &lefta);
601  } else {
602  sub_left_prediction_bgr32(s, s->temp[0], dst, width,
603  &leftr, &leftg, &leftb, &lefta);
604  }
605  encode_bgra_bitstream(s, width, 4);
606  }
607  } else if (avctx->pix_fmt == AV_PIX_FMT_RGB24) {
608  uint8_t *data = p->data[0] + (height - 1) * p->linesize[0];
609  const int stride = -p->linesize[0];
610  const int fake_stride = -fake_ystride;
611  int y;
612  int leftr, leftg, leftb;
613 
614  put_bits(&s->pb, 8, leftr = data[0]);
615  put_bits(&s->pb, 8, leftg = data[1]);
616  put_bits(&s->pb, 8, leftb = data[2]);
617  put_bits(&s->pb, 8, 0);
618 
619  sub_left_prediction_rgb24(s, s->temp[0], data + 3, width - 1,
620  &leftr, &leftg, &leftb);
621  encode_bgra_bitstream(s, width-1, 3);
622 
623  for (y = 1; y < s->height; y++) {
624  uint8_t *dst = data + y * stride;
625  if (s->predictor == PLANE && s->interlaced < y) {
626  s->dsp.diff_bytes(s->temp[1], dst, dst - fake_stride,
627  width * 3);
628  sub_left_prediction_rgb24(s, s->temp[0], s->temp[1], width,
629  &leftr, &leftg, &leftb);
630  } else {
631  sub_left_prediction_rgb24(s, s->temp[0], dst, width,
632  &leftr, &leftg, &leftb);
633  }
634  encode_bgra_bitstream(s, width, 3);
635  }
636  } else {
637  av_log(avctx, AV_LOG_ERROR, "Format not supported!\n");
638  }
639  emms_c();
640 
641  size += (put_bits_count(&s->pb) + 31) / 8;
642  put_bits(&s->pb, 16, 0);
643  put_bits(&s->pb, 15, 0);
644  size /= 4;
645 
646  if ((s->flags&CODEC_FLAG_PASS1) && (s->picture_number & 31) == 0) {
647  int j;
648  char *p = avctx->stats_out;
649  char *end = p + 1024*30;
650  for (i = 0; i < 3; i++) {
651  for (j = 0; j < 256; j++) {
652  snprintf(p, end-p, "%"PRIu64" ", s->stats[i][j]);
653  p += strlen(p);
654  s->stats[i][j]= 0;
655  }
656  snprintf(p, end-p, "\n");
657  p++;
658  }
659  } else
660  avctx->stats_out[0] = '\0';
661  if (!(s->avctx->flags2 & CODEC_FLAG2_NO_OUTPUT)) {
662  flush_put_bits(&s->pb);
663  s->dsp.bswap_buf((uint32_t*)pkt->data, (uint32_t*)pkt->data, size);
664  }
665 
666  s->picture_number++;
667 
668  pkt->size = size * 4;
669  pkt->flags |= AV_PKT_FLAG_KEY;
670  *got_packet = 1;
671 
672  return 0;
673 }
674 
676 {
677  HYuvContext *s = avctx->priv_data;
678 
680 
681  av_freep(&avctx->extradata);
682  av_freep(&avctx->stats_out);
683 
684  return 0;
685 }
686 
687 #if CONFIG_HUFFYUV_ENCODER
688 AVCodec ff_huffyuv_encoder = {
689  .name = "huffyuv",
690  .long_name = NULL_IF_CONFIG_SMALL("Huffyuv / HuffYUV"),
691  .type = AVMEDIA_TYPE_VIDEO,
692  .id = AV_CODEC_ID_HUFFYUV,
693  .priv_data_size = sizeof(HYuvContext),
694  .init = encode_init,
695  .encode2 = encode_frame,
696  .close = encode_end,
697  .pix_fmts = (const enum AVPixelFormat[]){
700  },
701 };
702 #endif
703 
704 #if CONFIG_FFVHUFF_ENCODER
705 AVCodec ff_ffvhuff_encoder = {
706  .name = "ffvhuff",
707  .long_name = NULL_IF_CONFIG_SMALL("Huffyuv FFmpeg variant"),
708  .type = AVMEDIA_TYPE_VIDEO,
709  .id = AV_CODEC_ID_FFVHUFF,
710  .priv_data_size = sizeof(HYuvContext),
711  .init = encode_init,
712  .encode2 = encode_frame,
713  .close = encode_end,
714  .pix_fmts = (const enum AVPixelFormat[]){
717  },
718 };
719 #endif
Definition: vf_geq.c:45
int ff_alloc_packet2(AVCodecContext *avctx, AVPacket *avpkt, int64_t size)
Check AVPacket size and/or allocate data.
Definition: utils.c:1500
const char const char void * val
Definition: avisynth_c.h:671
float v
const char * s
Definition: avisynth_c.h:668
int size
This structure describes decoded (raw) audio or video data.
Definition: frame.h:96
static av_cold int encode_init(AVCodecContext *avctx)
Definition: huffyuvenc.c:144
#define CODEC_FLAG_PASS2
Use internal 2pass ratecontrol in second pass mode.
Definition: avcodec.h:704
int bitstream_bpp
Definition: huffyuv.h:66
static void put_bits(Jpeg2000EncoderContext *s, int val, int n)
put n times val bit
Definition: j2kenc.c:160
else temp
Definition: vf_mcdeint.c:258
const char * g
Definition: vf_curves.c:104
#define CODEC_FLAG_PASS1
Use internal 2pass ratecontrol in first pass mode.
Definition: avcodec.h:703
static av_cold int init(AVCodecContext *avctx)
Definition: avrndec.c:35
packed RGB 8:8:8, 24bpp, RGBRGB...
Definition: avcodec.h:4536
planar YUV 4:2:2, 16bpp, (1 Cr &amp; Cb sample per 2x1 Y samples)
Definition: avcodec.h:4538
int size
Definition: avcodec.h:1064
const char * b
Definition: vf_curves.c:105
void ff_huff_gen_len_table(uint8_t *dst, const uint64_t *stats)
Definition: huffman.c:53
Definition: vf_geq.c:45
void av_log(void *avcl, int level, const char *fmt,...) av_printf_format(3
Send the specified message to the log if the level is less than or equal to the current av_log_level...
enum AVPixelFormat pix_fmt
Pixel format, see AV_PIX_FMT_xxx.
Definition: avcodec.h:1342
int context
Definition: huffyuv.h:72
Pixel format.
Definition: avcodec.h:4533
int stride
Definition: mace.c:144
AVCodec.
Definition: avcodec.h:2922
#define av_cold
Definition: avcodec.h:653
int height
Definition: huffyuv.h:70
uint8_t len[3][256]
Definition: huffyuv.h:77
uint8_t * extradata
some codecs need / can use extradata like Huffman tables.
Definition: avcodec.h:1254
#define LOAD_GBRA
void av_freep(void *ptr)
Free a memory block which has been allocated with av_malloc(z)() or av_realloc() and set the pointer ...
Definition: mem.c:234
int context_model
context model
Definition: avcodec.h:2269
uint8_t
static const uint8_t offset[511][2]
Definition: vf_uspp.c:58
static av_cold int end(AVCodecContext *avctx)
Definition: avrndec.c:67
#define emms_c()
Definition: internal.h:49
const char * name
Name of the codec implementation.
Definition: avcodec.h:2929
const char data[16]
Definition: mxf.c:68
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:92
int bits_per_coded_sample
bits per sample/pixel from the demuxer (needed for huffyuv).
Definition: avcodec.h:2563
#define LOAD2
#define A(x)
Definition: vp56_arith.h:28
#define AV_PKT_FLAG_KEY
The packet contains a keyframe.
Definition: avcodec.h:1113
void(* diff_bytes)(uint8_t *dst, const uint8_t *src1, const uint8_t *src2, int w)
Definition: dsputil.h:195
av_cold int ff_huffyuv_alloc_temp(HYuvContext *s)
Definition: huffyuv.c:57
enum AVCodecID id
Definition: avcodec.h:2936
uint64_t stats[3][256]
Definition: huffyuv.h:76
static double alpha(void *priv, double x, double y)
Definition: vf_geq.c:98
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: avcodec.h:4147
Definition: huffyuv.h:55
av_cold void ff_huffyuv_common_end(HYuvContext *s)
Definition: huffyuv.c:90
void(* sub_hfyu_median_prediction)(uint8_t *dst, const uint8_t *src1, const uint8_t *src2, int w, int *left, int *left_top)
subtract huffyuv&#39;s variant of median prediction note, this might read from src1[-1], src2[-1]
Definition: dsputil.h:200
#define CODEC_FLAG_INTERLACED_ME
interlaced motion estimation
Definition: avcodec.h:718
void(* bswap_buf)(uint32_t *dst, const uint32_t *src, int w)
Definition: dsputil.h:205
int flags
Definition: huffyuv.h:71
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification. ...
Definition: internal.h:151
const char * r
Definition: vf_curves.c:103
struct AVCodec * codec
Definition: avcodec.h:1155
int flags
CODEC_FLAG_*.
Definition: avcodec.h:1234
uint8_t * buf
Definition: put_bits.h:44
Definition: vf_geq.c:45
Libavcodec external API header.
AVPixelFormat
Pixel format.
Definition: pixfmt.h:66
huffyuv codec for libavcodec.
int flags
A combination of AV_PKT_FLAG values.
Definition: avcodec.h:1069
static int put_bits_count(PutBitContext *s)
Definition: put_bits.h:73
uint32_t bits[3][256]
Definition: huffyuv.h:78
#define WRITE_GBRA
#define WRITE2
int decorrelate
Definition: huffyuv.h:65
enum AVPictureType pict_type
Picture type of the frame.
Definition: frame.h:167
int width
Definition: huffyuv.h:70
float y
#define FF_MIN_BUFFER_SIZE
minimum encoding buffer size Used to avoid some checks during header writing.
Definition: avcodec.h:587
char * stats_in
pass2 encoding statistics input buffer Concatenated stuff from stats_out of pass1 should be placed he...
Definition: avcodec.h:2384
ret
Definition: avfilter.c:961
#define FFMIN(a, b)
Definition: avcodec.h:925
#define AV_PIX_FMT_RGB32
Definition: avcodec.h:4928
float u
uint8_t * temp[3]
Definition: huffyuv.h:75
char * stats_out
pass1 encoding statistics output buffer
Definition: avcodec.h:2376
static av_cold int encode_end(AVCodecContext *avctx)
Definition: huffyuvenc.c:675
int AC3_NAME() encode_frame(AVCodecContext *avctx, AVPacket *avpkt, const AVFrame *frame, int *got_packet_ptr)
planar YUV 4:2:0, 12bpp, (1 Cr &amp; Cb sample per 2x2 Y samples)
Definition: avcodec.h:4534
static int width
Definition: utils.c:158
#define AV_LOG_INFO
Standard information.
Definition: avcodec.h:4158
int picture_number
Definition: huffyuv.h:73
AVS_Value src
Definition: avisynth_c.h:523
static int sub_left_prediction(HYuvContext *s, uint8_t *dst, const uint8_t *src, int w, int left)
Definition: huffyuvenc.c:35
static int encode_422_bitstream(HYuvContext *s, int offset, int count)
Definition: huffyuvenc.c:288
main external API structure.
Definition: avcodec.h:1146
static void close(AVCodecParserContext *s)
Definition: h264_parser.c:538
#define STAT2
uint8_t * buf_end
Definition: put_bits.h:44
void * buf
Definition: avisynth_c.h:594
int interlaced
Definition: huffyuv.h:64
int extradata_size
Definition: avcodec.h:1255
BYTE int const BYTE int int int height
Definition: avisynth_c.h:713
int index
Definition: gxfenc.c:89
AVFrame * coded_frame
the picture in the bitstream
Definition: avcodec.h:2588
uint8_t * data
Definition: avcodec.h:1063
huffman tree builder and VLC generator
int linesize[AV_NUM_DATA_POINTERS]
For video, size in bytes of each picture line.
Definition: frame.h:124
static int encode_bgra_bitstream(HYuvContext *s, int count, int planes)
Definition: huffyuvenc.c:388
#define LOAD4
#define STAT_BGRA
#define snprintf
Definition: snprintf.h:34
void * priv_data
Definition: avcodec.h:1182
int version
Definition: huffyuv.h:67
Predictor predictor
Definition: huffyuv.h:61
#define v0
Definition: regdef.h:26
AVCodecContext * avctx
Definition: huffyuv.h:60
PutBitContext pb
Definition: huffyuv.h:63
Definition: huffyuv.h:56
common internal api header.
static void flush_put_bits(PutBitContext *s)
Pad the end of the output stream with zeros.
Definition: put_bits.h:89
#define CODEC_FLAG2_NO_OUTPUT
Skip bitstream encoding.
Definition: avcodec.h:721
static int store_table(HYuvContext *s, const uint8_t *len, uint8_t *buf)
Definition: huffyuvenc.c:120
int prediction_method
prediction method (needed for huffyuv)
Definition: avcodec.h:1498
static int encode_gray_bitstream(HYuvContext *s, int count)
Definition: huffyuvenc.c:343
static void init_put_bits(PutBitContext *s, uint8_t *buffer, int buffer_size)
Initialize the PutBitContext s.
Definition: put_bits.h:54
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:57
av_cold void ff_huffyuv_common_init(AVCodecContext *avctx)
Definition: huffyuv.c:75
int len
int key_frame
1 -&gt; keyframe, 0-&gt; not
Definition: frame.h:162
#define AVERROR(e)
int flags2
CODEC_FLAG2_*.
Definition: avcodec.h:1241
AVFrame picture
Definition: huffyuv.h:81
void INT64 INT64 count
Definition: avisynth_c.h:594
#define av_assert0(cond)
assert() equivalent, that is always enabled.
Definition: avassert.h:37
static AVPacket pkt
Definition: demuxing.c:52
DSPContext dsp
Definition: huffyuv.h:84
This structure stores compressed data.
Definition: avcodec.h:1040
uint8_t * data[AV_NUM_DATA_POINTERS]
pointer to the picture/channel planes.
Definition: frame.h:107
for(j=16;j >0;--j)
int ff_huffyuv_generate_bits_table(uint32_t *dst, const uint8_t *len_table)
Definition: huffyuv.c:38
void * av_mallocz(size_t size) av_malloc_attrib 1(1)
Allocate a block of size bytes with alignment suitable for all memory accesses (including vectors if ...
Definition: mem.c:241
bitstream writer API