Libav
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 Libav.
8  *
9  * Libav 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  * Libav 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 Libav; if not, write to the Free Software
21  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
22  */
23 
29 #include "libavutil/opt.h"
30 
31 #include "avcodec.h"
32 #include "huffyuv.h"
33 #include "huffman.h"
34 #include "huffyuvencdsp.h"
35 #include "internal.h"
36 #include "put_bits.h"
37 
38 static inline int sub_left_prediction(HYuvContext *s, uint8_t *dst,
39  uint8_t *src, int w, int left)
40 {
41  int i;
42  if (w < 32) {
43  for (i = 0; i < w; i++) {
44  const int temp = src[i];
45  dst[i] = temp - left;
46  left = temp;
47  }
48  return left;
49  } else {
50  for (i = 0; i < 16; i++) {
51  const int temp = src[i];
52  dst[i] = temp - left;
53  left = temp;
54  }
55  s->hencdsp.diff_bytes(dst + 16, src + 16, src + 15, w - 16);
56  return src[w-1];
57  }
58 }
59 
60 static inline void sub_left_prediction_bgr32(HYuvContext *s, uint8_t *dst,
61  uint8_t *src, int w,
62  int *red, int *green, int *blue,
63  int *alpha)
64 {
65  int i;
66  int r, g, b, a;
67  r = *red;
68  g = *green;
69  b = *blue;
70  a = *alpha;
71 
72  for (i = 0; i < FFMIN(w, 4); i++) {
73  const int rt = src[i * 4 + R];
74  const int gt = src[i * 4 + G];
75  const int bt = src[i * 4 + B];
76  const int at = src[i * 4 + A];
77  dst[i * 4 + R] = rt - r;
78  dst[i * 4 + G] = gt - g;
79  dst[i * 4 + B] = bt - b;
80  dst[i * 4 + A] = at - a;
81  r = rt;
82  g = gt;
83  b = bt;
84  a = at;
85  }
86 
87  s->hencdsp.diff_bytes(dst + 16, src + 16, src + 12, w * 4 - 16);
88 
89  *red = src[(w - 1) * 4 + R];
90  *green = src[(w - 1) * 4 + G];
91  *blue = src[(w - 1) * 4 + B];
92  *alpha = src[(w - 1) * 4 + A];
93 }
94 
95 static inline void sub_left_prediction_rgb24(HYuvContext *s, uint8_t *dst,
96  uint8_t *src, int w,
97  int *red, int *green, int *blue)
98 {
99  int i;
100  int r, g, b;
101  r = *red;
102  g = *green;
103  b = *blue;
104  for (i = 0; i < FFMIN(w, 16); i++) {
105  const int rt = src[i * 3 + 0];
106  const int gt = src[i * 3 + 1];
107  const int bt = src[i * 3 + 2];
108  dst[i * 3 + 0] = rt - r;
109  dst[i * 3 + 1] = gt - g;
110  dst[i * 3 + 2] = bt - b;
111  r = rt;
112  g = gt;
113  b = bt;
114  }
115 
116  s->hencdsp.diff_bytes(dst + 48, src + 48, src + 48 - 3, w * 3 - 48);
117 
118  *red = src[(w - 1) * 3 + 0];
119  *green = src[(w - 1) * 3 + 1];
120  *blue = src[(w - 1) * 3 + 2];
121 }
122 
123 static int store_table(HYuvContext *s, const uint8_t *len, uint8_t *buf)
124 {
125  int i;
126  int index = 0;
127 
128  for (i = 0; i < 256;) {
129  int val = len[i];
130  int repeat = 0;
131 
132  for (; i < 256 && len[i] == val && repeat < 255; i++)
133  repeat++;
134 
135  assert(val < 32 && val >0 && repeat<256 && repeat>0);
136  if ( repeat > 7) {
137  buf[index++] = val;
138  buf[index++] = repeat;
139  } else {
140  buf[index++] = val | (repeat << 5);
141  }
142  }
143 
144  return index;
145 }
146 
148 {
149  HYuvContext *s = avctx->priv_data;
150  int i, j;
151 
152  ff_huffyuv_common_init(avctx);
154 
155  avctx->extradata = av_mallocz(1024*30); // 256*3+4 == 772
156  avctx->stats_out = av_mallocz(1024*30); // 21*256*3(%llu ) + 3(\n) + 1(0) = 16132
157  s->version = 2;
158 
159  if (!avctx->extradata || !avctx->stats_out)
160  return AVERROR(ENOMEM);
161 
162 #if FF_API_CODED_FRAME
165  avctx->coded_frame->key_frame = 1;
167 #endif
168 #if FF_API_PRIVATE_OPT
170  if (avctx->context_model == 1)
171  s->context = avctx->context_model;
173 #endif
174 
175  switch (avctx->pix_fmt) {
176  case AV_PIX_FMT_YUV420P:
177  case AV_PIX_FMT_YUV422P:
178  if (s->width & 1) {
179  av_log(avctx, AV_LOG_ERROR, "Width must be even for this colorspace.\n");
180  return -1;
181  }
182  s->bitstream_bpp = avctx->pix_fmt == AV_PIX_FMT_YUV420P ? 12 : 16;
183  break;
184  case AV_PIX_FMT_RGB32:
185  s->bitstream_bpp = 32;
186  break;
187  case AV_PIX_FMT_RGB24:
188  s->bitstream_bpp = 24;
189  break;
190  default:
191  av_log(avctx, AV_LOG_ERROR, "format not supported\n");
192  return -1;
193  }
195  s->decorrelate = s->bitstream_bpp >= 24;
196 #if FF_API_PRIVATE_OPT
198  if (avctx->prediction_method)
199  s->predictor = avctx->prediction_method;
201 #endif
202  s->interlaced = avctx->flags & AV_CODEC_FLAG_INTERLACED_ME ? 1 : 0;
203  if (s->context) {
205  av_log(avctx, AV_LOG_ERROR,
206  "context=1 is not compatible with "
207  "2 pass huffyuv encoding\n");
208  return -1;
209  }
210  }
211 
212  if (avctx->codec->id == AV_CODEC_ID_HUFFYUV) {
213  if (avctx->pix_fmt == AV_PIX_FMT_YUV420P) {
214  av_log(avctx, AV_LOG_ERROR,
215  "Error: YV12 is not supported by huffyuv; use "
216  "vcodec=ffvhuff or format=422p\n");
217  return -1;
218  }
219 #if FF_API_PRIVATE_OPT
220  if (s->context) {
221  av_log(avctx, AV_LOG_ERROR,
222  "Error: per-frame huffman tables are not supported "
223  "by huffyuv; use vcodec=ffvhuff\n");
224  return -1;
225  }
226 #endif
227  if (s->interlaced != ( s->height > 288 ))
228  av_log(avctx, AV_LOG_INFO,
229  "using huffyuv 2.2.0 or newer interlacing flag\n");
230  }
231 
232  if (s->bitstream_bpp >= 24 && s->predictor == MEDIAN) {
233  av_log(avctx, AV_LOG_ERROR,
234  "Error: RGB is incompatible with median predictor\n");
235  return -1;
236  }
237 
238  ((uint8_t*)avctx->extradata)[0] = s->predictor | (s->decorrelate << 6);
239  ((uint8_t*)avctx->extradata)[1] = s->bitstream_bpp;
240  ((uint8_t*)avctx->extradata)[2] = s->interlaced ? 0x10 : 0x20;
241  if (s->context)
242  ((uint8_t*)avctx->extradata)[2] |= 0x40;
243  ((uint8_t*)avctx->extradata)[3] = 0;
244  s->avctx->extradata_size = 4;
245 
246  if (avctx->stats_in) {
247  char *p = avctx->stats_in;
248 
249  for (i = 0; i < 3; i++)
250  for (j = 0; j < 256; j++)
251  s->stats[i][j] = 1;
252 
253  for (;;) {
254  for (i = 0; i < 3; i++) {
255  char *next;
256 
257  for (j = 0; j < 256; j++) {
258  s->stats[i][j] += strtol(p, &next, 0);
259  if (next == p) return -1;
260  p = next;
261  }
262  }
263  if (p[0] == 0 || p[1] == 0 || p[2] == 0) break;
264  }
265  } else {
266  for (i = 0; i < 3; i++)
267  for (j = 0; j < 256; j++) {
268  int d = FFMIN(j, 256 - j);
269 
270  s->stats[i][j] = 100000000 / (d + 1);
271  }
272  }
273 
274  for (i = 0; i < 3; i++) {
275  ff_huff_gen_len_table(s->len[i], s->stats[i]);
276 
277  if (ff_huffyuv_generate_bits_table(s->bits[i], s->len[i]) < 0) {
278  return -1;
279  }
280 
281  s->avctx->extradata_size +=
282  store_table(s, s->len[i], &((uint8_t*)s->avctx->extradata)[s->avctx->extradata_size]);
283  }
284 
285  if (s->context) {
286  for (i = 0; i < 3; i++) {
287  int pels = s->width * s->height / (i ? 40 : 10);
288  for (j = 0; j < 256; j++) {
289  int d = FFMIN(j, 256 - j);
290  s->stats[i][j] = pels/(d + 1);
291  }
292  }
293  } else {
294  for (i = 0; i < 3; i++)
295  for (j = 0; j < 256; j++)
296  s->stats[i][j]= 0;
297  }
298 
300 
301  s->picture_number=0;
302 
303  return 0;
304 }
305 static int encode_422_bitstream(HYuvContext *s, int offset, int count)
306 {
307  int i;
308  const uint8_t *y = s->temp[0] + offset;
309  const uint8_t *u = s->temp[1] + offset / 2;
310  const uint8_t *v = s->temp[2] + offset / 2;
311 
312  if (s->pb.buf_end - s->pb.buf - (put_bits_count(&s->pb) >> 3) < 2 * 4 * count) {
313  av_log(s->avctx, AV_LOG_ERROR, "encoded frame too large\n");
314  return -1;
315  }
316 
317 #define LOAD4\
318  int y0 = y[2 * i];\
319  int y1 = y[2 * i + 1];\
320  int u0 = u[i];\
321  int v0 = v[i];
322 
323  count /= 2;
324 
325  if (s->flags & AV_CODEC_FLAG_PASS1) {
326  for(i = 0; i < count; i++) {
327  LOAD4;
328  s->stats[0][y0]++;
329  s->stats[1][u0]++;
330  s->stats[0][y1]++;
331  s->stats[2][v0]++;
332  }
333  }
335  return 0;
336  if (s->context) {
337  for (i = 0; i < count; i++) {
338  LOAD4;
339  s->stats[0][y0]++;
340  put_bits(&s->pb, s->len[0][y0], s->bits[0][y0]);
341  s->stats[1][u0]++;
342  put_bits(&s->pb, s->len[1][u0], s->bits[1][u0]);
343  s->stats[0][y1]++;
344  put_bits(&s->pb, s->len[0][y1], s->bits[0][y1]);
345  s->stats[2][v0]++;
346  put_bits(&s->pb, s->len[2][v0], s->bits[2][v0]);
347  }
348  } else {
349  for(i = 0; i < count; i++) {
350  LOAD4;
351  put_bits(&s->pb, s->len[0][y0], s->bits[0][y0]);
352  put_bits(&s->pb, s->len[1][u0], s->bits[1][u0]);
353  put_bits(&s->pb, s->len[0][y1], s->bits[0][y1]);
354  put_bits(&s->pb, s->len[2][v0], s->bits[2][v0]);
355  }
356  }
357  return 0;
358 }
359 
360 static int encode_gray_bitstream(HYuvContext *s, int count)
361 {
362  int i;
363 
364  if (s->pb.buf_end - s->pb.buf - (put_bits_count(&s->pb) >> 3) < 4 * count) {
365  av_log(s->avctx, AV_LOG_ERROR, "encoded frame too large\n");
366  return -1;
367  }
368 
369 #define LOAD2\
370  int y0 = s->temp[0][2 * i];\
371  int y1 = s->temp[0][2 * i + 1];
372 #define STAT2\
373  s->stats[0][y0]++;\
374  s->stats[0][y1]++;
375 #define WRITE2\
376  put_bits(&s->pb, s->len[0][y0], s->bits[0][y0]);\
377  put_bits(&s->pb, s->len[0][y1], s->bits[0][y1]);
378 
379  count /= 2;
380 
381  if (s->flags & AV_CODEC_FLAG_PASS1) {
382  for (i = 0; i < count; i++) {
383  LOAD2;
384  STAT2;
385  }
386  }
388  return 0;
389 
390  if (s->context) {
391  for (i = 0; i < count; i++) {
392  LOAD2;
393  STAT2;
394  WRITE2;
395  }
396  } else {
397  for (i = 0; i < count; i++) {
398  LOAD2;
399  WRITE2;
400  }
401  }
402  return 0;
403 }
404 
405 static inline int encode_bgra_bitstream(HYuvContext *s, int count, int planes)
406 {
407  int i;
408 
409  if (s->pb.buf_end - s->pb.buf - (put_bits_count(&s->pb) >> 3) <
410  4 * planes * count) {
411  av_log(s->avctx, AV_LOG_ERROR, "encoded frame too large\n");
412  return -1;
413  }
414 
415 #define LOAD_GBRA \
416  int g = s->temp[0][planes == 3 ? 3 * i + 1 : 4 * i + G]; \
417  int b = s->temp[0][planes == 3 ? 3 * i + 2 : 4 * i + B] - g & 0xFF; \
418  int r = s->temp[0][planes == 3 ? 3 * i + 0 : 4 * i + R] - g & 0xFF; \
419  int a = s->temp[0][planes * i + A];
420 
421 #define STAT_BGRA \
422  s->stats[0][b]++; \
423  s->stats[1][g]++; \
424  s->stats[2][r]++; \
425  if (planes == 4) \
426  s->stats[2][a]++;
427 
428 #define WRITE_GBRA \
429  put_bits(&s->pb, s->len[1][g], s->bits[1][g]); \
430  put_bits(&s->pb, s->len[0][b], s->bits[0][b]); \
431  put_bits(&s->pb, s->len[2][r], s->bits[2][r]); \
432  if (planes == 4) \
433  put_bits(&s->pb, s->len[2][a], s->bits[2][a]);
434 
435  if ((s->flags & AV_CODEC_FLAG_PASS1) &&
437  for (i = 0; i < count; i++) {
438  LOAD_GBRA;
439  STAT_BGRA;
440  }
441  } else if (s->context || (s->flags & AV_CODEC_FLAG_PASS1)) {
442  for (i = 0; i < count; i++) {
443  LOAD_GBRA;
444  STAT_BGRA;
445  WRITE_GBRA;
446  }
447  } else {
448  for (i = 0; i < count; i++) {
449  LOAD_GBRA;
450  WRITE_GBRA;
451  }
452  }
453  return 0;
454 }
455 
456 static int encode_frame(AVCodecContext *avctx, AVPacket *pkt,
457  const AVFrame *pict, int *got_packet)
458 {
459  HYuvContext *s = avctx->priv_data;
460  const int width = s->width;
461  const int width2 = s->width>>1;
462  const int height = s->height;
463  const int fake_ystride = s->interlaced ? pict->linesize[0]*2 : pict->linesize[0];
464  const int fake_ustride = s->interlaced ? pict->linesize[1]*2 : pict->linesize[1];
465  const int fake_vstride = s->interlaced ? pict->linesize[2]*2 : pict->linesize[2];
466  const AVFrame * const p = pict;
467  int i, j, size = 0, ret;
468 
469  if (!pkt->data &&
470  (ret = av_new_packet(pkt, width * height * 3 * 4 + AV_INPUT_BUFFER_MIN_SIZE)) < 0) {
471  av_log(avctx, AV_LOG_ERROR, "Error allocating output packet.\n");
472  return ret;
473  }
474 
475  if (s->context) {
476  for (i = 0; i < 3; i++) {
477  ff_huff_gen_len_table(s->len[i], s->stats[i]);
478  if (ff_huffyuv_generate_bits_table(s->bits[i], s->len[i]) < 0)
479  return -1;
480  size += store_table(s, s->len[i], &pkt->data[size]);
481  }
482 
483  for (i = 0; i < 3; i++)
484  for (j = 0; j < 256; j++)
485  s->stats[i][j] >>= 1;
486  }
487 
488  init_put_bits(&s->pb, pkt->data + size, pkt->size - size);
489 
490  if (avctx->pix_fmt == AV_PIX_FMT_YUV422P ||
491  avctx->pix_fmt == AV_PIX_FMT_YUV420P) {
492  int lefty, leftu, leftv, y, cy;
493 
494  put_bits(&s->pb, 8, leftv = p->data[2][0]);
495  put_bits(&s->pb, 8, lefty = p->data[0][1]);
496  put_bits(&s->pb, 8, leftu = p->data[1][0]);
497  put_bits(&s->pb, 8, p->data[0][0]);
498 
499  lefty = sub_left_prediction(s, s->temp[0], p->data[0], width , 0);
500  leftu = sub_left_prediction(s, s->temp[1], p->data[1], width2, 0);
501  leftv = sub_left_prediction(s, s->temp[2], p->data[2], width2, 0);
502 
503  encode_422_bitstream(s, 2, width-2);
504 
505  if (s->predictor==MEDIAN) {
506  int lefttopy, lefttopu, lefttopv;
507  cy = y = 1;
508  if (s->interlaced) {
509  lefty = sub_left_prediction(s, s->temp[0], p->data[0] + p->linesize[0], width , lefty);
510  leftu = sub_left_prediction(s, s->temp[1], p->data[1] + p->linesize[1], width2, leftu);
511  leftv = sub_left_prediction(s, s->temp[2], p->data[2] + p->linesize[2], width2, leftv);
512 
513  encode_422_bitstream(s, 0, width);
514  y++; cy++;
515  }
516 
517  lefty = sub_left_prediction(s, s->temp[0], p->data[0] + fake_ystride, 4, lefty);
518  leftu = sub_left_prediction(s, s->temp[1], p->data[1] + fake_ustride, 2, leftu);
519  leftv = sub_left_prediction(s, s->temp[2], p->data[2] + fake_vstride, 2, leftv);
520 
521  encode_422_bitstream(s, 0, 4);
522 
523  lefttopy = p->data[0][3];
524  lefttopu = p->data[1][1];
525  lefttopv = p->data[2][1];
526  s->hencdsp.sub_hfyu_median_pred(s->temp[0], p->data[0] + 4, p->data[0] + fake_ystride + 4, width - 4, &lefty, &lefttopy);
527  s->hencdsp.sub_hfyu_median_pred(s->temp[1], p->data[1] + 2, p->data[1] + fake_ustride + 2, width2 - 2, &leftu, &lefttopu);
528  s->hencdsp.sub_hfyu_median_pred(s->temp[2], p->data[2] + 2, p->data[2] + fake_vstride + 2, width2 - 2, &leftv, &lefttopv);
529  encode_422_bitstream(s, 0, width - 4);
530  y++; cy++;
531 
532  for (; y < height; y++,cy++) {
533  uint8_t *ydst, *udst, *vdst;
534 
535  if (s->bitstream_bpp == 12) {
536  while (2 * cy > y) {
537  ydst = p->data[0] + p->linesize[0] * y;
538  s->hencdsp.sub_hfyu_median_pred(s->temp[0], ydst - fake_ystride, ydst, width, &lefty, &lefttopy);
539  encode_gray_bitstream(s, width);
540  y++;
541  }
542  if (y >= height) break;
543  }
544  ydst = p->data[0] + p->linesize[0] * y;
545  udst = p->data[1] + p->linesize[1] * cy;
546  vdst = p->data[2] + p->linesize[2] * cy;
547 
548  s->hencdsp.sub_hfyu_median_pred(s->temp[0], ydst - fake_ystride, ydst, width, &lefty, &lefttopy);
549  s->hencdsp.sub_hfyu_median_pred(s->temp[1], udst - fake_ustride, udst, width2, &leftu, &lefttopu);
550  s->hencdsp.sub_hfyu_median_pred(s->temp[2], vdst - fake_vstride, vdst, width2, &leftv, &lefttopv);
551 
552  encode_422_bitstream(s, 0, width);
553  }
554  } else {
555  for (cy = y = 1; y < height; y++, cy++) {
556  uint8_t *ydst, *udst, *vdst;
557 
558  /* encode a luma only line & y++ */
559  if (s->bitstream_bpp == 12) {
560  ydst = p->data[0] + p->linesize[0] * y;
561 
562  if (s->predictor == PLANE && s->interlaced < y) {
563  s->hencdsp.diff_bytes(s->temp[1], ydst, ydst - fake_ystride, width);
564 
565  lefty = sub_left_prediction(s, s->temp[0], s->temp[1], width , lefty);
566  } else {
567  lefty = sub_left_prediction(s, s->temp[0], ydst, width , lefty);
568  }
569  encode_gray_bitstream(s, width);
570  y++;
571  if (y >= height) break;
572  }
573 
574  ydst = p->data[0] + p->linesize[0] * y;
575  udst = p->data[1] + p->linesize[1] * cy;
576  vdst = p->data[2] + p->linesize[2] * cy;
577 
578  if (s->predictor == PLANE && s->interlaced < cy) {
579  s->hencdsp.diff_bytes(s->temp[1], ydst, ydst - fake_ystride, width);
580  s->hencdsp.diff_bytes(s->temp[2], udst, udst - fake_ustride, width2);
581  s->hencdsp.diff_bytes(s->temp[2] + width2, vdst, vdst - fake_vstride, width2);
582 
583  lefty = sub_left_prediction(s, s->temp[0], s->temp[1], width , lefty);
584  leftu = sub_left_prediction(s, s->temp[1], s->temp[2], width2, leftu);
585  leftv = sub_left_prediction(s, s->temp[2], s->temp[2] + width2, width2, leftv);
586  } else {
587  lefty = sub_left_prediction(s, s->temp[0], ydst, width , lefty);
588  leftu = sub_left_prediction(s, s->temp[1], udst, width2, leftu);
589  leftv = sub_left_prediction(s, s->temp[2], vdst, width2, leftv);
590  }
591 
592  encode_422_bitstream(s, 0, width);
593  }
594  }
595  } else if(avctx->pix_fmt == AV_PIX_FMT_RGB32) {
596  uint8_t *data = p->data[0] + (height - 1) * p->linesize[0];
597  const int stride = -p->linesize[0];
598  const int fake_stride = -fake_ystride;
599  int y;
600  int leftr, leftg, leftb, lefta;
601 
602  put_bits(&s->pb, 8, lefta = data[A]);
603  put_bits(&s->pb, 8, leftr = data[R]);
604  put_bits(&s->pb, 8, leftg = data[G]);
605  put_bits(&s->pb, 8, leftb = data[B]);
606 
607  sub_left_prediction_bgr32(s, s->temp[0], data + 4, width - 1,
608  &leftr, &leftg, &leftb, &lefta);
609  encode_bgra_bitstream(s, width - 1, 4);
610 
611  for (y = 1; y < s->height; y++) {
612  uint8_t *dst = data + y*stride;
613  if (s->predictor == PLANE && s->interlaced < y) {
614  s->hencdsp.diff_bytes(s->temp[1], dst, dst - fake_stride, width * 4);
615  sub_left_prediction_bgr32(s, s->temp[0], s->temp[1], width,
616  &leftr, &leftg, &leftb, &lefta);
617  } else {
618  sub_left_prediction_bgr32(s, s->temp[0], dst, width,
619  &leftr, &leftg, &leftb, &lefta);
620  }
621  encode_bgra_bitstream(s, width, 4);
622  }
623  } else if (avctx->pix_fmt == AV_PIX_FMT_RGB24) {
624  uint8_t *data = p->data[0] + (height - 1) * p->linesize[0];
625  const int stride = -p->linesize[0];
626  const int fake_stride = -fake_ystride;
627  int y;
628  int leftr, leftg, leftb;
629 
630  put_bits(&s->pb, 8, leftr = data[0]);
631  put_bits(&s->pb, 8, leftg = data[1]);
632  put_bits(&s->pb, 8, leftb = data[2]);
633  put_bits(&s->pb, 8, 0);
634 
635  sub_left_prediction_rgb24(s, s->temp[0], data + 3, width - 1,
636  &leftr, &leftg, &leftb);
637  encode_bgra_bitstream(s, width-1, 3);
638 
639  for (y = 1; y < s->height; y++) {
640  uint8_t *dst = data + y * stride;
641  if (s->predictor == PLANE && s->interlaced < y) {
642  s->hencdsp.diff_bytes(s->temp[1], dst, dst - fake_stride,
643  width * 3);
644  sub_left_prediction_rgb24(s, s->temp[0], s->temp[1], width,
645  &leftr, &leftg, &leftb);
646  } else {
647  sub_left_prediction_rgb24(s, s->temp[0], dst, width,
648  &leftr, &leftg, &leftb);
649  }
650  encode_bgra_bitstream(s, width, 3);
651  }
652  } else {
653  av_log(avctx, AV_LOG_ERROR, "Format not supported!\n");
654  }
655  emms_c();
656 
657  size += (put_bits_count(&s->pb) + 31) / 8;
658  put_bits(&s->pb, 16, 0);
659  put_bits(&s->pb, 15, 0);
660  size /= 4;
661 
662  if ((s->flags & AV_CODEC_FLAG_PASS1) && (s->picture_number & 31) == 0) {
663  int j;
664  char *p = avctx->stats_out;
665  char *end = p + 1024*30;
666  for (i = 0; i < 3; i++) {
667  for (j = 0; j < 256; j++) {
668  snprintf(p, end-p, "%"PRIu64" ", s->stats[i][j]);
669  p += strlen(p);
670  s->stats[i][j]= 0;
671  }
672  snprintf(p, end-p, "\n");
673  p++;
674  }
675  } else
676  avctx->stats_out[0] = '\0';
677  if (!(s->avctx->flags2 & AV_CODEC_FLAG2_NO_OUTPUT)) {
678  flush_put_bits(&s->pb);
679  s->bdsp.bswap_buf((uint32_t *) pkt->data, (uint32_t *) pkt->data, size);
680  }
681 
682  s->picture_number++;
683 
684  pkt->size = size * 4;
685  pkt->flags |= AV_PKT_FLAG_KEY;
686  *got_packet = 1;
687 
688  return 0;
689 }
690 
692 {
693  HYuvContext *s = avctx->priv_data;
694 
696 
697  av_freep(&avctx->extradata);
698  av_freep(&avctx->stats_out);
699 
700  return 0;
701 }
702 
703 #define OFFSET(x) offsetof(HYuvContext, x)
704 #define VE AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_ENCODING_PARAM
705 
706 #define HUFF_CLASS(variant) \
707 static const AVClass variant ## _class = { \
708  .class_name = # variant, \
709  .item_name = av_default_item_name, \
710  .option = variant ## _options, \
711  .version = LIBAVUTIL_VERSION_INT, \
712 }
713 
714 #define FF_HUFFYUV_COMMON_OPTS \
715 { "pred", "Prediction method", OFFSET(predictor), AV_OPT_TYPE_INT, { .i64 = LEFT }, LEFT, MEDIAN, VE, "pred" }, \
716  { "left", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = LEFT }, INT_MIN, INT_MAX, VE, "pred" }, \
717  { "plane", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = PLANE }, INT_MIN, INT_MAX, VE, "pred" }, \
718  { "median", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = MEDIAN }, INT_MIN, INT_MAX, VE, "pred" }
719 
720 static const AVOption huffyuv_options[] = {
722  { NULL},
723 };
724 
725 HUFF_CLASS(huffyuv);
726 
728  .name = "huffyuv",
729  .long_name = NULL_IF_CONFIG_SMALL("Huffyuv / HuffYUV"),
730  .type = AVMEDIA_TYPE_VIDEO,
731  .id = AV_CODEC_ID_HUFFYUV,
732  .priv_data_size = sizeof(HYuvContext),
733  .priv_class = &huffyuv_class,
734  .init = encode_init,
735  .encode2 = encode_frame,
736  .close = encode_end,
737  .pix_fmts = (const enum AVPixelFormat[]){
740  },
741  .caps_internal = FF_CODEC_CAP_INIT_THREADSAFE |
743 };
744 
745 #if CONFIG_FFVHUFF_ENCODER
746 static const AVOption ffhuffyuv_options[] = {
748  { "context", "Set per-frame huffman tables", OFFSET(context), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, 1, VE },
749  { NULL }
750 };
751 
752 HUFF_CLASS(ffhuffyuv);
753 
754 AVCodec ff_ffvhuff_encoder = {
755  .name = "ffvhuff",
756  .long_name = NULL_IF_CONFIG_SMALL("Huffyuv FFmpeg variant"),
757  .type = AVMEDIA_TYPE_VIDEO,
758  .id = AV_CODEC_ID_FFVHUFF,
759  .priv_data_size = sizeof(HYuvContext),
760  .priv_class = &ffhuffyuv_class,
761  .init = encode_init,
762  .encode2 = encode_frame,
763  .close = encode_end,
764  .pix_fmts = (const enum AVPixelFormat[]){
767  },
768  .caps_internal = FF_CODEC_CAP_INIT_THREADSAFE |
770 };
771 #endif
#define FF_CODEC_CAP_INIT_CLEANUP
The codec allows calling the close function for deallocation even if the init function returned a fai...
Definition: internal.h:48
#define AV_CODEC_FLAG_INTERLACED_ME
interlaced motion estimation
Definition: avcodec.h:797
#define G
Definition: huffyuv.h:50
const struct AVCodec * codec
Definition: avcodec.h:1418
int size
This structure describes decoded (raw) audio or video data.
Definition: frame.h:140
static av_cold int encode_init(AVCodecContext *avctx)
Definition: huffyuvenc.c:147
AVOption.
Definition: opt.h:234
static int sub_left_prediction(HYuvContext *s, uint8_t *dst, uint8_t *src, int w, int left)
Definition: huffyuvenc.c:38
void(* diff_bytes)(uint8_t *dst, uint8_t *src1, uint8_t *src2, int w)
Definition: huffyuvencdsp.h:25
int bitstream_bpp
Definition: huffyuv.h:69
packed RGB 8:8:8, 24bpp, RGBRGB...
Definition: pixfmt.h:61
#define R
Definition: huffyuv.h:51
int size
Definition: avcodec.h:1347
av_log(ac->avr, AV_LOG_TRACE, "%d samples - audio_convert: %s to %s (%s)\, len, av_get_sample_fmt_name(ac->in_fmt), av_get_sample_fmt_name(ac->out_fmt), use_generic ? ac->func_descr_generic :ac->func_descr)
void ff_huff_gen_len_table(uint8_t *dst, const uint64_t *stats)
Definition: huffman.c:57
enum AVPixelFormat pix_fmt
Pixel format, see AV_PIX_FMT_xxx.
Definition: avcodec.h:1621
char * stats_in
pass2 encoding statistics input buffer Concatenated stuff from stats_out of pass1 should be placed he...
Definition: avcodec.h:2564
int context
Definition: huffyuv.h:75
void ff_huffyuv_common_end(HYuvContext *s)
Definition: huffyuv.c:90
int stride
Definition: mace.c:144
AVCodec.
Definition: avcodec.h:3120
int height
Definition: huffyuv.h:73
uint8_t len[3][256]
Definition: huffyuv.h:80
av_cold void ff_huffyuvencdsp_init(HuffYUVEncDSPContext *c)
Definition: huffyuvencdsp.c:77
#define LOAD_GBRA
void av_freep(void *arg)
Free a memory block which has been allocated with av_malloc(z)() or av_realloc() and set the pointer ...
Definition: mem.c:202
#define FF_CODEC_CAP_INIT_THREADSAFE
The codec does not modify any global variables in the init function, allowing to call the init functi...
Definition: internal.h:40
uint8_t
#define av_cold
Definition: attributes.h:66
AVOptions.
Definition: vf_drawbox.c:37
void(* bswap_buf)(uint32_t *dst, const uint32_t *src, int w)
Definition: bswapdsp.h:25
#define b
Definition: input.c:52
#define emms_c()
Definition: internal.h:48
uint8_t * extradata
some codecs need / can use extradata like Huffman tables.
Definition: avcodec.h:1523
const char data[16]
Definition: mxf.c:70
uint8_t * data
Definition: avcodec.h:1346
attribute_deprecated int context_model
Definition: avcodec.h:2446
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:95
#define B
Definition: huffyuv.h:49
int bits_per_coded_sample
bits per sample/pixel from the demuxer (needed for huffyuv).
Definition: avcodec.h:2769
#define LOAD2
char * stats_out
pass1 encoding statistics output buffer
Definition: avcodec.h:2556
#define AV_INPUT_BUFFER_MIN_SIZE
minimum encoding buffer size Used to avoid some checks during header writing.
Definition: avcodec.h:645
#define r
Definition: input.c:51
#define AV_PKT_FLAG_KEY
The packet contains a keyframe.
Definition: avcodec.h:1378
#define src
Definition: vp8dsp.c:254
static int encode_frame(AVCodecContext *avctx, AVPacket *pkt, const AVFrame *pict, int *got_packet)
Definition: huffyuvenc.c:456
int av_new_packet(AVPacket *pkt, int size)
Allocate the payload of a packet and initialize its fields with default values.
Definition: avpacket.c:84
av_cold int ff_huffyuv_alloc_temp(HYuvContext *s)
Definition: huffyuv.c:58
enum AVCodecID id
Definition: avcodec.h:3134
uint64_t stats[3][256]
Definition: huffyuv.h:79
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:124
Definition: huffyuv.h:57
#define AVERROR(e)
Definition: error.h:43
int flags
Definition: huffyuv.h:74
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification. ...
Definition: internal.h:148
g
Definition: yuv2rgb.c:546
int flags
AV_CODEC_FLAG_*.
Definition: avcodec.h:1503
uint8_t * buf
Definition: put_bits.h:38
const char * name
Name of the codec implementation.
Definition: avcodec.h:3127
static void put_bits(PutBitContext *s, int n, unsigned int value)
Write up to 31 bits into a bitstream.
Definition: put_bits.h:134
huffyuv codec for libavcodec.
int flags
A combination of AV_PKT_FLAG values.
Definition: avcodec.h:1352
static int put_bits_count(PutBitContext *s)
Definition: put_bits.h:67
planar YUV 4:2:2, 16bpp, (1 Cr & Cb sample per 2x1 Y samples)
Definition: pixfmt.h:63
uint32_t bits[3][256]
Definition: huffyuv.h:81
#define WRITE_GBRA
#define WRITE2
int decorrelate
Definition: huffyuv.h:68
enum AVPictureType pict_type
Picture type of the frame.
Definition: frame.h:201
#define FFMIN(a, b)
Definition: common.h:66
int width
Definition: huffyuv.h:73
AVCodec ff_huffyuv_encoder
Definition: huffyuvenc.c:727
#define AV_CODEC_FLAG_PASS1
Use internal 2pass ratecontrol in first pass mode.
Definition: avcodec.h:751
uint8_t * temp[3]
Definition: huffyuv.h:78
static const AVOption huffyuv_options[]
Definition: huffyuvenc.c:720
static av_cold int encode_end(AVCodecContext *avctx)
Definition: huffyuvenc.c:691
NULL
Definition: eval.c:55
static int width
Definition: utils.c:156
#define AV_LOG_INFO
Standard information.
Definition: log.h:135
void(* sub_hfyu_median_pred)(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.
Definition: huffyuvencdsp.h:33
int picture_number
Definition: huffyuv.h:76
Libavcodec external API header.
static int encode_422_bitstream(HYuvContext *s, int offset, int count)
Definition: huffyuvenc.c:305
attribute_deprecated int prediction_method
Definition: avcodec.h:1784
int linesize[AV_NUM_DATA_POINTERS]
For video, size in bytes of each picture line.
Definition: frame.h:158
main external API structure.
Definition: avcodec.h:1409
#define AV_PIX_FMT_RGB32
Definition: pixfmt.h:242
#define STAT2
uint8_t * buf_end
Definition: put_bits.h:38
int interlaced
Definition: huffyuv.h:67
int extradata_size
Definition: avcodec.h:1524
int index
Definition: gxfenc.c:72
huffman tree builder and VLC generator
static int encode_bgra_bitstream(HYuvContext *s, int count, int planes)
Definition: huffyuvenc.c:405
#define LOAD4
#define STAT_BGRA
#define u(width,...)
static enum AVPixelFormat pix_fmts[]
Definition: libkvazaar.c:257
HuffYUVEncDSPContext hencdsp
Definition: huffyuv.h:88
int version
Definition: huffyuv.h:70
uint8_t * data[AV_NUM_DATA_POINTERS]
pointer to the picture/channel planes.
Definition: frame.h:146
Predictor predictor
Definition: huffyuv.h:64
int height
Definition: gxfenc.c:72
AVCodecContext * avctx
Definition: huffyuv.h:63
PutBitContext pb
Definition: huffyuv.h:66
planar YUV 4:2:0, 12bpp, (1 Cr & Cb sample per 2x2 Y samples)
Definition: pixfmt.h:59
Definition: huffyuv.h:58
#define OFFSET(x)
Definition: huffyuvenc.c:703
#define FF_DISABLE_DEPRECATION_WARNINGS
Definition: internal.h:77
common internal api header.
static void flush_put_bits(PutBitContext *s)
Pad the end of the output stream with zeros.
Definition: put_bits.h:83
#define VE
Definition: huffyuvenc.c:704
static int store_table(HYuvContext *s, const uint8_t *len, uint8_t *buf)
Definition: huffyuvenc.c:123
static void sub_left_prediction_bgr32(HYuvContext *s, uint8_t *dst, uint8_t *src, int w, int *red, int *green, int *blue, int *alpha)
Definition: huffyuvenc.c:60
#define AV_CODEC_FLAG2_NO_OUTPUT
Skip bitstream encoding.
Definition: avcodec.h:807
attribute_deprecated AVFrame * coded_frame
the picture in the bitstream
Definition: avcodec.h:2797
static int encode_gray_bitstream(HYuvContext *s, int count)
Definition: huffyuvenc.c:360
static void init_put_bits(PutBitContext *s, uint8_t *buffer, int buffer_size)
Initialize the PutBitContext s.
Definition: put_bits.h:48
static av_cold int init(AVCodecParserContext *s)
Definition: h264_parser.c:582
av_cold void ff_huffyuv_common_init(AVCodecContext *avctx)
Definition: huffyuv.c:76
#define AV_CODEC_FLAG_PASS2
Use internal 2pass ratecontrol in second pass mode.
Definition: avcodec.h:755
void * priv_data
Definition: avcodec.h:1451
#define FF_ENABLE_DEPRECATION_WARNINGS
Definition: internal.h:78
int len
int key_frame
1 -> keyframe, 0-> not
Definition: frame.h:196
int flags2
AV_CODEC_FLAG2_*.
Definition: avcodec.h:1510
#define FF_HUFFYUV_COMMON_OPTS
Definition: huffyuvenc.c:714
AVPixelFormat
Pixel format.
Definition: pixfmt.h:57
This structure stores compressed data.
Definition: avcodec.h:1323
BswapDSPContext bdsp
Definition: huffyuv.h:86
void * av_mallocz(size_t size)
Allocate a block of size bytes with alignment suitable for all memory accesses (including vectors if ...
Definition: mem.c:211
for(j=16;j >0;--j)
#define HUFF_CLASS(variant)
Definition: huffyuvenc.c:706
int ff_huffyuv_generate_bits_table(uint32_t *dst, const uint8_t *len_table)
Definition: huffyuv.c:39
bitstream writer API