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