FFmpeg
libxvid.c
Go to the documentation of this file.
1 /*
2  * Interface to xvidcore for MPEG-4 encoding
3  * Copyright (c) 2004 Adam Thayer <krevnik@comcast.net>
4  *
5  * This file is part of FFmpeg.
6  *
7  * FFmpeg is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU Lesser General Public
9  * License as published by the Free Software Foundation; either
10  * version 2.1 of the License, or (at your option) any later version.
11  *
12  * FFmpeg is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15  * Lesser General Public License for more details.
16  *
17  * You should have received a copy of the GNU Lesser General Public
18  * License along with FFmpeg; if not, write to the Free Software
19  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20  */
21 
22 /**
23  * @file
24  * Interface to xvidcore for MPEG-4 compliant encoding.
25  * @author Adam Thayer (krevnik@comcast.net)
26  */
27 
28 #include <stdio.h>
29 #include <string.h>
30 #include <xvid.h>
31 
32 #include "libavutil/avassert.h"
33 #include "libavutil/file_open.h"
34 #include "libavutil/internal.h"
35 #include "libavutil/intreadwrite.h"
36 #include "libavutil/mathematics.h"
37 #include "libavutil/mem.h"
38 #include "libavutil/opt.h"
39 
40 #include "avcodec.h"
41 #include "codec_internal.h"
42 #include "encode.h"
43 #include "mpegutils.h"
44 
45 #if HAVE_UNISTD_H
46 #include <unistd.h>
47 #endif
48 
49 #if HAVE_IO_H
50 #include <io.h>
51 #endif
52 
53 /**
54  * Buffer management macros.
55  */
56 #define BUFFER_SIZE 1024
57 #define BUFFER_REMAINING(x) (BUFFER_SIZE - strlen(x))
58 #define BUFFER_CAT(x) (&((x)[strlen(x)]))
59 
60 /**
61  * Structure for the private Xvid context.
62  * This stores all the private context for the codec.
63  */
64 struct xvid_context {
65  AVClass *class;
66  void *encoder_handle; /**< Handle for Xvid encoder */
67  int xsize; /**< Frame x size */
68  int ysize; /**< Frame y size */
69  int vop_flags; /**< VOP flags for Xvid encoder */
70  int vol_flags; /**< VOL flags for Xvid encoder */
71  int me_flags; /**< Motion Estimation flags */
72  int qscale; /**< Do we use constant scale? */
73  int quicktime_format; /**< Are we in a QT-based format? */
74  char *twopassbuffer; /**< Character buffer for two-pass */
75  char *old_twopassbuffer; /**< Old character buffer (two-pass) */
76  char *twopassfile; /**< second pass temp file name */
77  int twopassfd;
78  unsigned char *intra_matrix; /**< P-Frame Quant Matrix */
79  unsigned char *inter_matrix; /**< I-Frame Quant Matrix */
80  int lumi_aq; /**< Lumi masking as an aq method */
81  int variance_aq; /**< Variance adaptive quantization */
82  int ssim; /**< SSIM information display mode */
83  int ssim_acc; /**< SSIM accuracy. 0: accurate. 4: fast. */
84  int gmc;
85  int me_quality; /**< Motion estimation quality. 0: fast 6: best. */
86  int mpeg_quant; /**< Quantization type. 0: H.263, 1: MPEG */
87 };
88 
89 /**
90  * Structure for the private first-pass plugin.
91  */
92 struct xvid_ff_pass1 {
93  int version; /**< Xvid version */
94  struct xvid_context *context; /**< Pointer to private context */
95 };
96 
97 static int xvid_encode_close(AVCodecContext *avctx);
98 static int xvid_encode_frame(AVCodecContext *avctx, AVPacket *pkt,
99  const AVFrame *picture, int *got_packet);
100 
101 
102 /*
103  * Xvid 2-Pass Kludge Section
104  *
105  * Xvid's default 2-pass doesn't allow us to create data as we need to, so
106  * this section spends time replacing the first pass plugin so we can write
107  * statistic information as libavcodec requests in. We have another kludge
108  * that allows us to pass data to the second pass in Xvid without a custom
109  * rate-control plugin.
110  */
111 
112 /**
113  * Initialize the two-pass plugin and context.
114  *
115  * @param param Input construction parameter structure
116  * @param handle Private context handle
117  * @return Returns XVID_ERR_xxxx on failure, or 0 on success.
118  */
119 static int xvid_ff_2pass_create(xvid_plg_create_t *param, void **handle)
120 {
121  struct xvid_ff_pass1 *x = (struct xvid_ff_pass1 *) param->param;
122  char *log = x->context->twopassbuffer;
123 
124  /* Do a quick bounds check */
125  if (!log)
126  return XVID_ERR_FAIL;
127 
128  /* We use snprintf() */
129  /* This is because we can safely prevent a buffer overflow */
130  log[0] = 0;
132  "# ffmpeg 2-pass log file, using xvid codec\n");
134  "# Do not modify. libxvidcore version: %d.%d.%d\n\n",
135  XVID_VERSION_MAJOR(XVID_VERSION),
136  XVID_VERSION_MINOR(XVID_VERSION),
137  XVID_VERSION_PATCH(XVID_VERSION));
138 
139  *handle = x->context;
140  return 0;
141 }
142 
143 /**
144  * Destroy the two-pass plugin context.
145  *
146  * @param ref Context pointer for the plugin
147  * @param param Destroy context
148  * @return Returns 0, success guaranteed
149  */
151  xvid_plg_destroy_t *param)
152 {
153  /* Currently cannot think of anything to do on destruction */
154  /* Still, the framework should be here for reference/use */
155  if (ref->twopassbuffer)
156  ref->twopassbuffer[0] = 0;
157  return 0;
158 }
159 
160 /**
161  * Enable fast encode mode during the first pass.
162  *
163  * @param ref Context pointer for the plugin
164  * @param param Frame data
165  * @return Returns 0, success guaranteed
166  */
168  xvid_plg_data_t *param)
169 {
170  int motion_remove;
171  int motion_replacements;
172  int vop_remove;
173 
174  /* Nothing to do here, result is changed too much */
175  if (param->zone && param->zone->mode == XVID_ZONE_QUANT)
176  return 0;
177 
178  /* We can implement a 'turbo' first pass mode here */
179  param->quant = 2;
180 
181  /* Init values */
182  motion_remove = ~XVID_ME_CHROMA_PVOP &
183  ~XVID_ME_CHROMA_BVOP &
184  ~XVID_ME_EXTSEARCH16 &
185  ~XVID_ME_ADVANCEDDIAMOND16;
186  motion_replacements = XVID_ME_FAST_MODEINTERPOLATE |
187  XVID_ME_SKIP_DELTASEARCH |
188  XVID_ME_FASTREFINE16 |
189  XVID_ME_BFRAME_EARLYSTOP;
190  vop_remove = ~XVID_VOP_MODEDECISION_RD &
191  ~XVID_VOP_FAST_MODEDECISION_RD &
192  ~XVID_VOP_TRELLISQUANT &
193  ~XVID_VOP_INTER4V &
194  ~XVID_VOP_HQACPRED;
195 
196  param->vol_flags &= ~XVID_VOL_GMC;
197  param->vop_flags &= vop_remove;
198  param->motion_flags &= motion_remove;
199  param->motion_flags |= motion_replacements;
200 
201  return 0;
202 }
203 
204 /**
205  * Capture statistic data and write it during first pass.
206  *
207  * @param ref Context pointer for the plugin
208  * @param param Statistic data
209  * @return Returns XVID_ERR_xxxx on failure, or 0 on success
210  */
212  xvid_plg_data_t *param)
213 {
214  char *log = ref->twopassbuffer;
215  const char *frame_types = " ipbs";
216  char frame_type;
217 
218  /* Quick bounds check */
219  if (!log)
220  return XVID_ERR_FAIL;
221 
222  /* Convert the type given to us into a character */
223  if (param->type < 5 && param->type > 0)
224  frame_type = frame_types[param->type];
225  else
226  return XVID_ERR_FAIL;
227 
229  "%c %d %d %d %d %d %d\n",
230  frame_type, param->stats.quant, param->stats.kblks,
231  param->stats.mblks, param->stats.ublks,
232  param->stats.length, param->stats.hlength);
233 
234  return 0;
235 }
236 
237 /**
238  * Dispatch function for our custom plugin.
239  * This handles the dispatch for the Xvid plugin. It passes data
240  * on to other functions for actual processing.
241  *
242  * @param ref Context pointer for the plugin
243  * @param cmd The task given for us to complete
244  * @param p1 First parameter (varies)
245  * @param p2 Second parameter (varies)
246  * @return Returns XVID_ERR_xxxx on failure, or 0 on success
247  */
248 static int xvid_ff_2pass(void *ref, int cmd, void *p1, void *p2)
249 {
250  switch (cmd) {
251  case XVID_PLG_INFO:
252  case XVID_PLG_FRAME:
253  return 0;
254  case XVID_PLG_BEFORE:
255  return xvid_ff_2pass_before(ref, p1);
256  case XVID_PLG_CREATE:
257  return xvid_ff_2pass_create(p1, p2);
258  case XVID_PLG_AFTER:
259  return xvid_ff_2pass_after(ref, p1);
260  case XVID_PLG_DESTROY:
261  return xvid_ff_2pass_destroy(ref, p1);
262  default:
263  return XVID_ERR_FAIL;
264  }
265 }
266 
267 /**
268  * Routine to create a global VO/VOL header for MP4 container.
269  * What we do here is extract the header from the Xvid bitstream
270  * as it is encoded. We also strip the repeated headers from the
271  * bitstream when a global header is requested for MPEG-4 ISO
272  * compliance.
273  *
274  * @param avctx AVCodecContext pointer to context
275  * @param frame Pointer to encoded frame data
276  * @param header_len Length of header to search
277  * @param frame_len Length of encoded frame data
278  * @return Returns new length of frame data
279  */
281  unsigned int header_len,
282  unsigned int frame_len)
283 {
284  int vo_len = 0, i;
285 
286  for (i = 0; i < header_len - 3; i++) {
287  if (pkt->data[i] == 0x00 &&
288  pkt->data[i + 1] == 0x00 &&
289  pkt->data[i + 2] == 0x01 &&
290  pkt->data[i + 3] == 0xB6) {
291  vo_len = i;
292  break;
293  }
294  }
295 
296  if (vo_len > 0) {
297  /* We need to store the header, so extract it */
298  if (!avctx->extradata) {
299  avctx->extradata = av_malloc(vo_len);
300  if (!avctx->extradata)
301  return AVERROR(ENOMEM);
302  memcpy(avctx->extradata, pkt->data, vo_len);
303  avctx->extradata_size = vo_len;
304  }
305  /* Less dangerous now, memmove properly copies the two
306  * chunks of overlapping data */
307  memmove(pkt->data, &pkt->data[vo_len], frame_len - vo_len);
308  pkt->size = frame_len - vo_len;
309  }
310  return 0;
311 }
312 
313 /**
314  * Routine to correct a possibly erroneous framerate being fed to us.
315  * Xvid currently chokes on framerates where the ticks per frame is
316  * extremely large. This function works to correct problems in this area
317  * by estimating a new framerate and taking the simpler fraction of
318  * the two presented.
319  *
320  * @param avctx Context that contains the framerate to correct.
321  */
323 {
324  int frate, fbase;
325  int est_frate, est_fbase;
326  int gcd;
327  float est_fps, fps;
328 
329  frate = avctx->time_base.den;
330  fbase = avctx->time_base.num;
331 
332  gcd = av_gcd(frate, fbase);
333  if (gcd > 1) {
334  frate /= gcd;
335  fbase /= gcd;
336  }
337 
338  if (frate <= 65000 && fbase <= 65000) {
339  avctx->time_base.den = frate;
340  avctx->time_base.num = fbase;
341  return;
342  }
343 
344  fps = (float) frate / (float) fbase;
345  est_fps = roundf(fps * 1000.0) / 1000.0;
346 
347  est_frate = (int) est_fps;
348  if (est_fps > (int) est_fps) {
349  est_frate = (est_frate + 1) * 1000;
350  est_fbase = (int) roundf((float) est_frate / est_fps);
351  } else
352  est_fbase = 1;
353 
354  gcd = av_gcd(est_frate, est_fbase);
355  if (gcd > 1) {
356  est_frate /= gcd;
357  est_fbase /= gcd;
358  }
359 
360  if (fbase > est_fbase) {
361  avctx->time_base.den = est_frate;
362  avctx->time_base.num = est_fbase;
363  av_log(avctx, AV_LOG_DEBUG,
364  "Xvid: framerate re-estimated: %.2f, %.3f%% correction\n",
365  est_fps, (((est_fps - fps) / fps) * 100.0));
366  } else {
367  avctx->time_base.den = frate;
368  avctx->time_base.num = fbase;
369  }
370 }
371 
373 {
374  int xerr, i, ret = -1;
375  int xvid_flags = avctx->flags;
376  struct xvid_context *x = avctx->priv_data;
377  uint16_t *intra, *inter;
378  int fd;
379 
380  xvid_plugin_single_t single = { 0 };
381  struct xvid_ff_pass1 rc2pass1 = { 0 };
382  xvid_plugin_2pass2_t rc2pass2 = { 0 };
383  xvid_plugin_lumimasking_t masking_l = { 0 }; /* For lumi masking */
384  xvid_plugin_lumimasking_t masking_v = { 0 }; /* For variance AQ */
385  xvid_plugin_ssim_t ssim = { 0 };
386  xvid_gbl_init_t xvid_gbl_init = { 0 };
387  xvid_enc_create_t xvid_enc_create = { 0 };
388  xvid_enc_plugin_t plugins[4];
389 
390  x->twopassfd = -1;
391 
392  /* Bring in VOP flags from ffmpeg command-line */
393  x->vop_flags = XVID_VOP_HALFPEL; /* Bare minimum quality */
394  if (xvid_flags & AV_CODEC_FLAG_4MV)
395  x->vop_flags |= XVID_VOP_INTER4V; /* Level 3 */
396  if (avctx->trellis)
397  x->vop_flags |= XVID_VOP_TRELLISQUANT; /* Level 5 */
398  if (xvid_flags & AV_CODEC_FLAG_AC_PRED)
399  x->vop_flags |= XVID_VOP_HQACPRED; /* Level 6 */
400  if (xvid_flags & AV_CODEC_FLAG_GRAY)
401  x->vop_flags |= XVID_VOP_GREYSCALE;
402 
403  /* Decide which ME quality setting to use */
404  x->me_flags = 0;
405  switch (x->me_quality) {
406  case 6:
407  case 5:
408  x->me_flags |= XVID_ME_EXTSEARCH16 |
409  XVID_ME_EXTSEARCH8;
411  case 4:
412  case 3:
413  x->me_flags |= XVID_ME_ADVANCEDDIAMOND8 |
414  XVID_ME_HALFPELREFINE8 |
415  XVID_ME_CHROMA_PVOP |
416  XVID_ME_CHROMA_BVOP;
418  case 2:
419  case 1:
420  x->me_flags |= XVID_ME_ADVANCEDDIAMOND16 |
421  XVID_ME_HALFPELREFINE16;
422  }
423 
424  /* Decide how we should decide blocks */
425  switch (avctx->mb_decision) {
426  case FF_MB_DECISION_RD:
427  x->vop_flags |= XVID_VOP_MODEDECISION_RD;
428  x->me_flags |= XVID_ME_HALFPELREFINE8_RD |
429  XVID_ME_QUARTERPELREFINE8_RD |
430  XVID_ME_EXTSEARCH_RD |
431  XVID_ME_CHECKPREDICTION_RD;
433  case FF_MB_DECISION_BITS:
434  if (!(x->vop_flags & XVID_VOP_MODEDECISION_RD))
435  x->vop_flags |= XVID_VOP_FAST_MODEDECISION_RD;
436  x->me_flags |= XVID_ME_HALFPELREFINE16_RD |
437  XVID_ME_QUARTERPELREFINE16_RD;
439  default:
440  break;
441  }
442 
443  /* Bring in VOL flags from ffmpeg command-line */
444  x->vol_flags = 0;
445  if (x->gmc) {
446  x->vol_flags |= XVID_VOL_GMC;
447  x->me_flags |= XVID_ME_GME_REFINE;
448  }
449  if (xvid_flags & AV_CODEC_FLAG_QPEL) {
450  x->vol_flags |= XVID_VOL_QUARTERPEL;
451  x->me_flags |= XVID_ME_QUARTERPELREFINE16;
452  if (x->vop_flags & XVID_VOP_INTER4V)
453  x->me_flags |= XVID_ME_QUARTERPELREFINE8;
454  }
455 
456  xvid_gbl_init.version = XVID_VERSION;
457  xvid_gbl_init.debug = 0;
458  xvid_gbl_init.cpu_flags = 0;
459 
460  /* Initialize */
461  xvid_global(NULL, XVID_GBL_INIT, &xvid_gbl_init, NULL);
462 
463  /* Create the encoder reference */
464  xvid_enc_create.version = XVID_VERSION;
465 
466  /* Store the desired frame size */
467  xvid_enc_create.width =
468  x->xsize = avctx->width;
469  xvid_enc_create.height =
470  x->ysize = avctx->height;
471 
472  /* Xvid can determine the proper profile to use */
473  /* xvid_enc_create.profile = XVID_PROFILE_S_L3; */
474 
475  /* We don't use zones */
476  xvid_enc_create.zones = NULL;
477  xvid_enc_create.num_zones = 0;
478 
479  xvid_enc_create.num_threads = avctx->thread_count;
480 #if (XVID_VERSION <= 0x010303) && (XVID_VERSION >= 0x010300)
481  /* workaround for a bug in libxvidcore */
482  if (avctx->height <= 16) {
483  if (avctx->thread_count < 2) {
484  xvid_enc_create.num_threads = 0;
485  } else {
486  av_log(avctx, AV_LOG_ERROR,
487  "Too small height for threads > 1.");
488  return AVERROR(EINVAL);
489  }
490  }
491 #endif
492 
493  xvid_enc_create.plugins = plugins;
494  xvid_enc_create.num_plugins = 0;
495 
496  /* Initialize Buffers */
497  x->twopassbuffer = NULL;
498  x->old_twopassbuffer = NULL;
499  x->twopassfile = NULL;
500 
501  if (xvid_flags & AV_CODEC_FLAG_PASS1) {
502  rc2pass1.version = XVID_VERSION;
503  rc2pass1.context = x;
506  if (!x->twopassbuffer || !x->old_twopassbuffer) {
507  av_log(avctx, AV_LOG_ERROR,
508  "Xvid: Cannot allocate 2-pass log buffers\n");
509  return AVERROR(ENOMEM);
510  }
511  x->twopassbuffer[0] =
512  x->old_twopassbuffer[0] = 0;
513 
514  plugins[xvid_enc_create.num_plugins].func = xvid_ff_2pass;
515  plugins[xvid_enc_create.num_plugins].param = &rc2pass1;
516  xvid_enc_create.num_plugins++;
517  } else if (xvid_flags & AV_CODEC_FLAG_PASS2) {
518  rc2pass2.version = XVID_VERSION;
519  rc2pass2.bitrate = avctx->bit_rate;
520 
521  fd = avpriv_tempfile("xvidff.", &x->twopassfile, 0, avctx);
522  if (fd < 0) {
523  av_log(avctx, AV_LOG_ERROR, "Xvid: Cannot write 2-pass pipe\n");
524  return fd;
525  }
526  x->twopassfd = fd;
527 
528  if (!avctx->stats_in) {
529  av_log(avctx, AV_LOG_ERROR,
530  "Xvid: No 2-pass information loaded for second pass\n");
531  return AVERROR(EINVAL);
532  }
533 
534  ret = write(fd, avctx->stats_in, strlen(avctx->stats_in));
535  if (ret == -1)
536  ret = AVERROR(errno);
537  else if (strlen(avctx->stats_in) > ret) {
538  av_log(avctx, AV_LOG_ERROR, "Xvid: Cannot write to 2-pass pipe\n");
539  ret = AVERROR(EIO);
540  }
541  if (ret < 0)
542  return ret;
543 
544  rc2pass2.filename = x->twopassfile;
545  plugins[xvid_enc_create.num_plugins].func = xvid_plugin_2pass2;
546  plugins[xvid_enc_create.num_plugins].param = &rc2pass2;
547  xvid_enc_create.num_plugins++;
548  } else if (!(xvid_flags & AV_CODEC_FLAG_QSCALE)) {
549  /* Single Pass Bitrate Control! */
550  single.version = XVID_VERSION;
551  single.bitrate = avctx->bit_rate;
552 
553  plugins[xvid_enc_create.num_plugins].func = xvid_plugin_single;
554  plugins[xvid_enc_create.num_plugins].param = &single;
555  xvid_enc_create.num_plugins++;
556  }
557 
558  if (avctx->lumi_masking != 0.0)
559  x->lumi_aq = 1;
560 
561  /* Luminance Masking */
562  if (x->lumi_aq) {
563  masking_l.method = 0;
564  plugins[xvid_enc_create.num_plugins].func = xvid_plugin_lumimasking;
565 
566  /* The old behavior is that when avctx->lumi_masking is specified,
567  * plugins[...].param = NULL. Trying to keep the old behavior here. */
568  plugins[xvid_enc_create.num_plugins].param =
569  avctx->lumi_masking ? NULL : &masking_l;
570  xvid_enc_create.num_plugins++;
571  }
572 
573  /* Variance AQ */
574  if (x->variance_aq) {
575  masking_v.method = 1;
576  plugins[xvid_enc_create.num_plugins].func = xvid_plugin_lumimasking;
577  plugins[xvid_enc_create.num_plugins].param = &masking_v;
578  xvid_enc_create.num_plugins++;
579  }
580 
581  if (x->lumi_aq && x->variance_aq )
582  av_log(avctx, AV_LOG_INFO,
583  "Both lumi_aq and variance_aq are enabled. The resulting quality"
584  "will be the worse one of the two effects made by the AQ.\n");
585 
586  /* SSIM */
587  if (x->ssim) {
588  plugins[xvid_enc_create.num_plugins].func = xvid_plugin_ssim;
589  ssim.b_printstat = x->ssim == 2;
590  ssim.acc = x->ssim_acc;
591  ssim.cpu_flags = xvid_gbl_init.cpu_flags;
592  ssim.b_visualize = 0;
593  plugins[xvid_enc_create.num_plugins].param = &ssim;
594  xvid_enc_create.num_plugins++;
595  }
596 
597  /* Frame Rate and Key Frames */
598  xvid_correct_framerate(avctx);
599  xvid_enc_create.fincr = avctx->time_base.num;
600  xvid_enc_create.fbase = avctx->time_base.den;
601  if (avctx->gop_size > 0)
602  xvid_enc_create.max_key_interval = avctx->gop_size;
603  else
604  xvid_enc_create.max_key_interval = 240; /* Xvid's best default */
605 
606  /* Quants */
607  if (xvid_flags & AV_CODEC_FLAG_QSCALE)
608  x->qscale = 1;
609  else
610  x->qscale = 0;
611 
612  xvid_enc_create.min_quant[0] = avctx->qmin;
613  xvid_enc_create.min_quant[1] = avctx->qmin;
614  xvid_enc_create.min_quant[2] = avctx->qmin;
615  xvid_enc_create.max_quant[0] = avctx->qmax;
616  xvid_enc_create.max_quant[1] = avctx->qmax;
617  xvid_enc_create.max_quant[2] = avctx->qmax;
618 
619  /* Quant Matrices */
620  x->intra_matrix =
621  x->inter_matrix = NULL;
622 
624  if (ret < 0)
625  return ret;
626 
627  if (x->mpeg_quant)
628  x->vol_flags |= XVID_VOL_MPEGQUANT;
629  if ((avctx->intra_matrix || avctx->inter_matrix)) {
630  x->vol_flags |= XVID_VOL_MPEGQUANT;
631 
632  if (avctx->intra_matrix) {
633  intra = avctx->intra_matrix;
634  x->intra_matrix = av_malloc(sizeof(unsigned char) * 64);
635  if (!x->intra_matrix)
636  return AVERROR(ENOMEM);
637  } else
638  intra = NULL;
639  if (avctx->inter_matrix) {
640  inter = avctx->inter_matrix;
641  x->inter_matrix = av_malloc(sizeof(unsigned char) * 64);
642  if (!x->inter_matrix)
643  return AVERROR(ENOMEM);
644  } else
645  inter = NULL;
646 
647  for (i = 0; i < 64; i++) {
648  if (intra)
649  x->intra_matrix[i] = (unsigned char) intra[i];
650  if (inter)
651  x->inter_matrix[i] = (unsigned char) inter[i];
652  }
653  }
654 
655  /* Misc Settings */
656  xvid_enc_create.frame_drop_ratio = 0;
657  xvid_enc_create.global = 0;
658  if (xvid_flags & AV_CODEC_FLAG_CLOSED_GOP)
659  xvid_enc_create.global |= XVID_GLOBAL_CLOSED_GOP;
660 
661  /* Determines which codec mode we are operating in */
662  avctx->extradata = NULL;
663  avctx->extradata_size = 0;
664  if (xvid_flags & AV_CODEC_FLAG_GLOBAL_HEADER) {
665  /* In this case, we are claiming to be MPEG-4 */
666  x->quicktime_format = 1;
667  } else {
668  /* We are claiming to be Xvid */
669  x->quicktime_format = 0;
670  if (!avctx->codec_tag)
671  avctx->codec_tag = AV_RL32("xvid");
672  }
673 
674  /* Bframes */
675  xvid_enc_create.max_bframes = avctx->max_b_frames;
676  xvid_enc_create.bquant_offset = 100 * avctx->b_quant_offset;
677  xvid_enc_create.bquant_ratio = 100 * avctx->b_quant_factor;
678  if (avctx->max_b_frames > 0 && !x->quicktime_format)
679  xvid_enc_create.global |= XVID_GLOBAL_PACKED;
680 
681  av_assert0(xvid_enc_create.num_plugins + (!!x->ssim) + (!!x->variance_aq) + (!!x->lumi_aq) <= FF_ARRAY_ELEMS(plugins));
682 
683  /* Encode a dummy frame to get the extradata immediately */
684  if (x->quicktime_format) {
685  AVFrame *picture;
686  AVPacket *packet;
687  int size, got_packet;
688 
689  packet = av_packet_alloc();
690  if (!packet)
691  return AVERROR(ENOMEM);
692 
693  picture = av_frame_alloc();
694  if (!picture) {
695  av_packet_free(&packet);
696  return AVERROR(ENOMEM);
697  }
698 
699  xerr = xvid_encore(NULL, XVID_ENC_CREATE, &xvid_enc_create, NULL);
700  if( xerr ) {
701  av_packet_free(&packet);
702  av_frame_free(&picture);
703  av_log(avctx, AV_LOG_ERROR, "Xvid: Could not create encoder reference\n");
704  return AVERROR_EXTERNAL;
705  }
706  x->encoder_handle = xvid_enc_create.handle;
707  size = ((avctx->width + 1) & ~1) * ((avctx->height + 1) & ~1);
708  picture->data[0] = av_malloc(size + size / 2);
709  if (!picture->data[0]) {
710  av_packet_free(&packet);
711  av_frame_free(&picture);
712  return AVERROR(ENOMEM);
713  }
714  picture->data[1] = picture->data[0] + size;
715  picture->data[2] = picture->data[1] + size / 4;
716  memset(picture->data[0], 0, size);
717  memset(picture->data[1], 128, size / 2);
718  xvid_encode_frame(avctx, packet, picture, &got_packet);
719  av_packet_free(&packet);
720  av_free(picture->data[0]);
721  av_frame_free(&picture);
722  xvid_encore(x->encoder_handle, XVID_ENC_DESTROY, NULL, NULL);
723  }
724 
725  /* Create encoder context */
726  xerr = xvid_encore(NULL, XVID_ENC_CREATE, &xvid_enc_create, NULL);
727  if (xerr) {
728  av_log(avctx, AV_LOG_ERROR, "Xvid: Could not create encoder reference\n");
729  return AVERROR_EXTERNAL;
730  }
731 
732  x->encoder_handle = xvid_enc_create.handle;
733 
734  return 0;
735 }
736 
738  const AVFrame *picture, int *got_packet)
739 {
740  int xerr, i, ret;
741  struct xvid_context *x = avctx->priv_data;
742  int mb_width = (avctx->width + 15) / 16;
743  int mb_height = (avctx->height + 15) / 16;
744  char *tmp;
745 
746  xvid_enc_frame_t xvid_enc_frame = { 0 };
747  xvid_enc_stats_t xvid_enc_stats = { 0 };
748 
749  if ((ret = ff_alloc_packet(avctx, pkt, mb_width*(int64_t)mb_height*MAX_MB_BYTES + FF_INPUT_BUFFER_MIN_SIZE)) < 0)
750  return ret;
751 
752  /* Start setting up the frame */
753  xvid_enc_frame.version = XVID_VERSION;
754  xvid_enc_stats.version = XVID_VERSION;
755 
756  /* Let Xvid know where to put the frame. */
757  xvid_enc_frame.bitstream = pkt->data;
758  xvid_enc_frame.length = pkt->size;
759 
760  /* Initialize input image fields */
761  if (avctx->pix_fmt != AV_PIX_FMT_YUV420P) {
762  av_log(avctx, AV_LOG_ERROR,
763  "Xvid: Color spaces other than 420P not supported\n");
764  return AVERROR(EINVAL);
765  }
766 
767  xvid_enc_frame.input.csp = XVID_CSP_PLANAR; /* YUV420P */
768 
769  for (i = 0; i < 4; i++) {
770  xvid_enc_frame.input.plane[i] = picture->data[i];
771  xvid_enc_frame.input.stride[i] = picture->linesize[i];
772  }
773 
774  /* Encoder Flags */
775  xvid_enc_frame.vop_flags = x->vop_flags;
776  xvid_enc_frame.vol_flags = x->vol_flags;
777  xvid_enc_frame.motion = x->me_flags;
778  xvid_enc_frame.type =
779  picture->pict_type == AV_PICTURE_TYPE_I ? XVID_TYPE_IVOP :
780  picture->pict_type == AV_PICTURE_TYPE_P ? XVID_TYPE_PVOP :
781  picture->pict_type == AV_PICTURE_TYPE_B ? XVID_TYPE_BVOP :
782  XVID_TYPE_AUTO;
783 
784  /* Pixel aspect ratio setting */
785  if (avctx->sample_aspect_ratio.num < 0 || avctx->sample_aspect_ratio.num > 255 ||
786  avctx->sample_aspect_ratio.den < 0 || avctx->sample_aspect_ratio.den > 255) {
787  av_log(avctx, AV_LOG_WARNING,
788  "Invalid pixel aspect ratio %i/%i, limit is 255/255 reducing\n",
791  avctx->sample_aspect_ratio.num, avctx->sample_aspect_ratio.den, 255);
792  }
793  xvid_enc_frame.par = XVID_PAR_EXT;
794  xvid_enc_frame.par_width = avctx->sample_aspect_ratio.num;
795  xvid_enc_frame.par_height = avctx->sample_aspect_ratio.den;
796 
797  /* Quant Setting */
798  if (x->qscale)
799  xvid_enc_frame.quant = picture->quality / FF_QP2LAMBDA;
800  else
801  xvid_enc_frame.quant = 0;
802 
803  /* Matrices */
804  xvid_enc_frame.quant_intra_matrix = x->intra_matrix;
805  xvid_enc_frame.quant_inter_matrix = x->inter_matrix;
806 
807  /* Encode */
808  xerr = xvid_encore(x->encoder_handle, XVID_ENC_ENCODE,
809  &xvid_enc_frame, &xvid_enc_stats);
810 
811  /* Two-pass log buffer swapping */
812  avctx->stats_out = NULL;
813  if (x->twopassbuffer) {
814  tmp = x->old_twopassbuffer;
816  x->twopassbuffer = tmp;
817  x->twopassbuffer[0] = 0;
818  if (x->old_twopassbuffer[0] != 0) {
819  avctx->stats_out = x->old_twopassbuffer;
820  }
821  }
822 
823  if (xerr > 0) {
824  enum AVPictureType pict_type;
825 
826  *got_packet = 1;
827 
828  if (xvid_enc_stats.type == XVID_TYPE_PVOP)
829  pict_type = AV_PICTURE_TYPE_P;
830  else if (xvid_enc_stats.type == XVID_TYPE_BVOP)
831  pict_type = AV_PICTURE_TYPE_B;
832  else if (xvid_enc_stats.type == XVID_TYPE_SVOP)
833  pict_type = AV_PICTURE_TYPE_S;
834  else
835  pict_type = AV_PICTURE_TYPE_I;
836 
837  ff_encode_add_stats_side_data(pkt, xvid_enc_stats.quant * FF_QP2LAMBDA, NULL, 0, pict_type);
838 
839  if (xvid_enc_frame.out_flags & XVID_KEYFRAME) {
841  if (x->quicktime_format)
842  return xvid_strip_vol_header(avctx, pkt,
843  xvid_enc_stats.hlength, xerr);
844  }
845 
846  pkt->size = xerr;
847 
848  return 0;
849  } else {
850  if (!xerr)
851  return 0;
852  av_log(avctx, AV_LOG_ERROR,
853  "Xvid: Encoding Error Occurred: %i\n", xerr);
854  return AVERROR_EXTERNAL;
855  }
856 }
857 
859 {
860  struct xvid_context *x = avctx->priv_data;
861 
862  if (x->encoder_handle) {
863  xvid_encore(x->encoder_handle, XVID_ENC_DESTROY, NULL, NULL);
864  x->encoder_handle = NULL;
865  }
866 
867  if (x->twopassbuffer) {
868  av_freep(&x->twopassbuffer);
870  avctx->stats_out = NULL;
871  }
872  if (x->twopassfd>=0) {
873  unlink(x->twopassfile);
874  close(x->twopassfd);
875  x->twopassfd = -1;
876  }
877  av_freep(&x->twopassfile);
878  av_freep(&x->intra_matrix);
879  av_freep(&x->inter_matrix);
880 
881  return 0;
882 }
883 
884 #define OFFSET(x) offsetof(struct xvid_context, x)
885 #define VE AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_ENCODING_PARAM
886 static const AVOption options[] = {
887  { "lumi_aq", "Luminance masking AQ", OFFSET(lumi_aq), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, 1, VE },
888  { "variance_aq", "Variance AQ", OFFSET(variance_aq), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, 1, VE },
889  { "ssim", "Show SSIM information to stdout", OFFSET(ssim), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, 2, VE, .unit = "ssim" },
890  { "off", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = 0 }, INT_MIN, INT_MAX, VE, .unit = "ssim" },
891  { "avg", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = 1 }, INT_MIN, INT_MAX, VE, .unit = "ssim" },
892  { "frame", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = 2 }, INT_MIN, INT_MAX, VE, .unit = "ssim" },
893  { "ssim_acc", "SSIM accuracy", OFFSET(ssim_acc), AV_OPT_TYPE_INT, { .i64 = 2 }, 0, 4, VE },
894  { "gmc", "use GMC", OFFSET(gmc), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, 1, VE },
895  { "me_quality", "Motion estimation quality", OFFSET(me_quality), AV_OPT_TYPE_INT, { .i64 = 4 }, 0, 6, VE },
896  { "mpeg_quant", "Use MPEG quantizers instead of H.263", OFFSET(mpeg_quant), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, 1, VE },
897  { NULL },
898 };
899 
900 static const AVClass xvid_class = {
901  .class_name = "libxvid",
902  .item_name = av_default_item_name,
903  .option = options,
904  .version = LIBAVUTIL_VERSION_INT,
905 };
906 
908  .p.name = "libxvid",
909  CODEC_LONG_NAME("libxvidcore MPEG-4 part 2"),
910  .p.type = AVMEDIA_TYPE_VIDEO,
911  .p.id = AV_CODEC_ID_MPEG4,
913  .priv_data_size = sizeof(struct xvid_context),
916  .close = xvid_encode_close,
918  .color_ranges = AVCOL_RANGE_MPEG,
919  .p.priv_class = &xvid_class,
920  .caps_internal = FF_CODEC_CAP_INIT_CLEANUP,
921  .p.wrapper_name = "libxvid",
922 };
FF_MATRIX_TYPE_INTRA
#define FF_MATRIX_TYPE_INTRA
Check if the elements of codec context matrices (intra_matrix, inter_matrix or chroma_intra_matrix) a...
Definition: encode.h:118
CODEC_PIXFMTS
#define CODEC_PIXFMTS(...)
Definition: codec_internal.h:392
xvid_context::encoder_handle
void * encoder_handle
Handle for Xvid encoder.
Definition: libxvid.c:66
AV_LOG_WARNING
#define AV_LOG_WARNING
Something somehow does not look correct.
Definition: log.h:216
FF_CODEC_CAP_INIT_CLEANUP
#define FF_CODEC_CAP_INIT_CLEANUP
The codec allows calling the close function for deallocation even if the init function returned a fai...
Definition: codec_internal.h:43
xvid_context::old_twopassbuffer
char * old_twopassbuffer
Old character buffer (two-pass)
Definition: libxvid.c:75
AVERROR
Filter the word “frame” indicates either a video frame or a group of audio as stored in an AVFrame structure Format for each input and each output the list of supported formats For video that means pixel format For audio that means channel sample they are references to shared objects When the negotiation mechanism computes the intersection of the formats supported at each end of a all references to both lists are replaced with a reference to the intersection And when a single format is eventually chosen for a link amongst the remaining all references to the list are updated That means that if a filter requires that its input and output have the same format amongst a supported all it has to do is use a reference to the same list of formats query_formats can leave some formats unset and return AVERROR(EAGAIN) to cause the negotiation mechanism toagain later. That can be used by filters with complex requirements to use the format negotiated on one link to set the formats supported on another. Frame references ownership and permissions
opt.h
xvid_context::xsize
int xsize
Frame x size.
Definition: libxvid.c:67
AVPictureType
AVPictureType
Definition: avutil.h:276
AV_CODEC_FLAG_QSCALE
#define AV_CODEC_FLAG_QSCALE
Use fixed qscale.
Definition: avcodec.h:213
int64_t
long long int64_t
Definition: coverity.c:34
AVCodecContext::intra_matrix
uint16_t * intra_matrix
custom intra quantization matrix Must be allocated with the av_malloc() family of functions,...
Definition: avcodec.h:956
xvid_context::qscale
int qscale
Do we use constant scale?
Definition: libxvid.c:72
normalize.log
log
Definition: normalize.py:21
AVCodecContext::lumi_masking
float lumi_masking
luminance masking (0-> disabled)
Definition: avcodec.h:816
AV_CODEC_ID_MPEG4
@ AV_CODEC_ID_MPEG4
Definition: codec_id.h:64
av_frame_free
void av_frame_free(AVFrame **frame)
Free the frame and any dynamically allocated objects in it, e.g.
Definition: frame.c:64
xvid_encode_frame
static int xvid_encode_frame(AVCodecContext *avctx, AVPacket *pkt, const AVFrame *picture, int *got_packet)
Definition: libxvid.c:737
AVFrame
This structure describes decoded (raw) audio or video data.
Definition: frame.h:435
AVPacket::data
uint8_t * data
Definition: packet.h:595
AVOption
AVOption.
Definition: opt.h:429
encode.h
AVCodecContext::b_quant_offset
float b_quant_offset
qscale offset between IP and B-frames
Definition: avcodec.h:793
FFCodec
Definition: codec_internal.h:127
BUFFER_REMAINING
#define BUFFER_REMAINING(x)
Definition: libxvid.c:57
xvid_strip_vol_header
static int xvid_strip_vol_header(AVCodecContext *avctx, AVPacket *pkt, unsigned int header_len, unsigned int frame_len)
Routine to create a global VO/VOL header for MP4 container.
Definition: libxvid.c:280
mathematics.h
xvid_context
Structure for the private Xvid context.
Definition: libxvid.c:64
AVCodecContext::mb_decision
int mb_decision
macroblock decision mode
Definition: avcodec.h:944
AVCodecContext::qmax
int qmax
maximum quantizer
Definition: avcodec.h:1253
mpegutils.h
AV_CODEC_FLAG_4MV
#define AV_CODEC_FLAG_4MV
4 MV per MB allowed / advanced prediction for H.263.
Definition: avcodec.h:217
AV_PKT_FLAG_KEY
#define AV_PKT_FLAG_KEY
The packet contains a keyframe.
Definition: packet.h:650
av_packet_free
void av_packet_free(AVPacket **pkt)
Free the packet, if the packet is reference counted, it will be unreferenced first.
Definition: packet.c:74
FF_INPUT_BUFFER_MIN_SIZE
#define FF_INPUT_BUFFER_MIN_SIZE
Used by some encoders as upper bound for the length of headers.
Definition: encode.h:33
roundf
static av_always_inline av_const float roundf(float x)
Definition: libm.h:453
AVFrame::data
uint8_t * data[AV_NUM_DATA_POINTERS]
pointer to the picture/channel planes.
Definition: frame.h:456
AV_CODEC_FLAG_GLOBAL_HEADER
#define AV_CODEC_FLAG_GLOBAL_HEADER
Place global headers in extradata instead of every keyframe.
Definition: avcodec.h:318
av_gcd
int64_t av_gcd(int64_t a, int64_t b)
Compute the greatest common divisor of two integer operands.
Definition: mathematics.c:37
close
static av_cold void close(AVCodecParserContext *s)
Definition: apv_parser.c:197
xvid_ff_pass1::context
struct xvid_context * context
Pointer to private context.
Definition: libxvid.c:94
FFCodec::p
AVCodec p
The public AVCodec.
Definition: codec_internal.h:131
AVCodecContext::thread_count
int thread_count
thread count is used to decide how many independent tasks should be passed to execute()
Definition: avcodec.h:1573
AVCodecContext::flags
int flags
AV_CODEC_FLAG_*.
Definition: avcodec.h:496
xvid_context::intra_matrix
unsigned char * intra_matrix
P-Frame Quant Matrix.
Definition: libxvid.c:78
ff_encode_add_stats_side_data
int ff_encode_add_stats_side_data(AVPacket *pkt, int quality, const int64_t error[], int error_count, enum AVPictureType pict_type)
Definition: encode.c:919
FF_CODEC_ENCODE_CB
#define FF_CODEC_ENCODE_CB(func)
Definition: codec_internal.h:359
av_reduce
int av_reduce(int *dst_num, int *dst_den, int64_t num, int64_t den, int64_t max)
Reduce a fraction.
Definition: rational.c:35
AVRational::num
int num
Numerator.
Definition: rational.h:59
av_frame_alloc
AVFrame * av_frame_alloc(void)
Allocate an AVFrame and set its fields to default values.
Definition: frame.c:52
avassert.h
AV_LOG_ERROR
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:210
FF_ARRAY_ELEMS
#define FF_ARRAY_ELEMS(a)
Definition: sinewin_tablegen.c:29
av_cold
#define av_cold
Definition: attributes.h:119
float
float
Definition: af_crystalizer.c:122
AVCodecContext::extradata_size
int extradata_size
Definition: avcodec.h:523
MAX_MB_BYTES
#define MAX_MB_BYTES
Definition: mpegutils.h:35
intreadwrite.h
xvid_context::twopassfile
char * twopassfile
second pass temp file name
Definition: libxvid.c:76
AVCodecContext::stats_in
char * stats_in
pass2 encoding statistics input buffer Concatenated stuff from stats_out of pass1 should be placed he...
Definition: avcodec.h:1332
xvid_context::ssim_acc
int ssim_acc
SSIM accuracy.
Definition: libxvid.c:83
AV_CODEC_CAP_ENCODER_REORDERED_OPAQUE
#define AV_CODEC_CAP_ENCODER_REORDERED_OPAQUE
This encoder can reorder user opaque values from input AVFrames and return them with corresponding ou...
Definition: codec.h:144
av_assert0
#define av_assert0(cond)
assert() equivalent, that is always enabled.
Definition: avassert.h:42
AV_LOG_DEBUG
#define AV_LOG_DEBUG
Stuff which is only useful for libav* developers.
Definition: log.h:231
OFFSET
#define OFFSET(x)
Definition: libxvid.c:884
AV_PIX_FMT_YUV420P
@ AV_PIX_FMT_YUV420P
planar YUV 4:2:0, 12bpp, (1 Cr & Cb sample per 2x2 Y samples)
Definition: pixfmt.h:73
av_fallthrough
#define av_fallthrough
Definition: attributes.h:67
file_open.h
CODEC_LONG_NAME
#define CODEC_LONG_NAME(str)
Definition: codec_internal.h:332
tmp
static uint8_t tmp[40]
Definition: aes_ctr.c:52
if
if(ret)
Definition: filter_design.txt:179
xvid_context::quicktime_format
int quicktime_format
Are we in a QT-based format?
Definition: libxvid.c:73
LIBAVUTIL_VERSION_INT
#define LIBAVUTIL_VERSION_INT
Definition: version.h:85
xvid_context::me_flags
int me_flags
Motion Estimation flags.
Definition: libxvid.c:71
AVClass
Describe the class of an AVClass context structure.
Definition: log.h:76
NULL
#define NULL
Definition: coverity.c:32
xvid_context::me_quality
int me_quality
Motion estimation quality.
Definition: libxvid.c:85
xvid_context::vop_flags
int vop_flags
VOP flags for Xvid encoder.
Definition: libxvid.c:69
AVCodecContext::bit_rate
int64_t bit_rate
the average bitrate
Definition: avcodec.h:489
av_default_item_name
const char * av_default_item_name(void *ptr)
Return the context name.
Definition: log.c:242
BUFFER_CAT
#define BUFFER_CAT(x)
Definition: libxvid.c:58
AV_PICTURE_TYPE_I
@ AV_PICTURE_TYPE_I
Intra.
Definition: avutil.h:278
options
Definition: swscale.c:45
AV_CODEC_FLAG_AC_PRED
#define AV_CODEC_FLAG_AC_PRED
H.263 advanced intra coding / MPEG-4 AC prediction.
Definition: avcodec.h:327
AVCodecContext::trellis
int trellis
trellis RD quantization
Definition: avcodec.h:1317
xvid_ff_2pass
static int xvid_ff_2pass(void *ref, int cmd, void *p1, void *p2)
Dispatch function for our custom plugin.
Definition: libxvid.c:248
xvid_context::twopassfd
int twopassfd
Definition: libxvid.c:77
options
static const AVOption options[]
Definition: libxvid.c:886
AVCodecContext::time_base
AVRational time_base
This is the fundamental unit of time (in seconds) in terms of which frame timestamps are represented.
Definition: avcodec.h:543
AVCodecContext::stats_out
char * stats_out
pass1 encoding statistics output buffer
Definition: avcodec.h:1324
AV_CODEC_FLAG_QPEL
#define AV_CODEC_FLAG_QPEL
Use qpel MC.
Definition: avcodec.h:225
avpriv_tempfile
int avpriv_tempfile(const char *prefix, char **filename, int log_offset, void *log_ctx)
Wrapper to work around the lack of mkstemp() on mingw.
Definition: file_open.c:111
AVFrame::pict_type
enum AVPictureType pict_type
Picture type of the frame.
Definition: frame.h:527
init
int(* init)(AVBSFContext *ctx)
Definition: dts2pts.c:551
AV_CODEC_CAP_DR1
#define AV_CODEC_CAP_DR1
Codec uses get_buffer() or get_encode_buffer() for allocating buffers and supports custom allocators.
Definition: codec.h:52
xvid_class
static const AVClass xvid_class
Definition: libxvid.c:900
AV_CODEC_FLAG_GRAY
#define AV_CODEC_FLAG_GRAY
Only decode/encode grayscale.
Definition: avcodec.h:302
AVPacket::size
int size
Definition: packet.h:596
AVCodecContext::gop_size
int gop_size
the number of pictures in a group of pictures, or 0 for intra_only
Definition: avcodec.h:1017
codec_internal.h
AVFrame::quality
int quality
quality (between 1 (good) and FF_LAMBDA_MAX (bad))
Definition: frame.h:557
i
#define i(width, name, range_min, range_max)
Definition: cbs_h264.c:63
xvid_ff_pass1::version
int version
Xvid version.
Definition: libxvid.c:93
size
int size
Definition: twinvq_data.h:10344
xvid_encode_close
static int xvid_encode_close(AVCodecContext *avctx)
Definition: libxvid.c:858
AV_CODEC_FLAG_PASS2
#define AV_CODEC_FLAG_PASS2
Use internal 2pass ratecontrol in second pass mode.
Definition: avcodec.h:294
xvid_context::variance_aq
int variance_aq
Variance adaptive quantization.
Definition: libxvid.c:81
AVERROR_EXTERNAL
#define AVERROR_EXTERNAL
Generic error in an external library.
Definition: error.h:59
AVPacket::flags
int flags
A combination of AV_PKT_FLAG values.
Definition: packet.h:601
av_packet_alloc
AVPacket * av_packet_alloc(void)
Allocate an AVPacket and set its fields to default values.
Definition: packet.c:63
AV_LOG_INFO
#define AV_LOG_INFO
Standard information.
Definition: log.h:221
AVCodecContext::b_quant_factor
float b_quant_factor
qscale factor between IP and B-frames If > 0 then the last P-frame quantizer will be used (q= lastp_q...
Definition: avcodec.h:786
BUFFER_SIZE
#define BUFFER_SIZE
Buffer management macros.
Definition: libxvid.c:56
xvid_context::vol_flags
int vol_flags
VOL flags for Xvid encoder.
Definition: libxvid.c:70
xvid_ff_2pass_before
static int xvid_ff_2pass_before(struct xvid_context *ref, xvid_plg_data_t *param)
Enable fast encode mode during the first pass.
Definition: libxvid.c:167
av_malloc
#define av_malloc(s)
Definition: ops_asmgen.c:44
frame_type
frame_type
Definition: jpeg2000_parser.c:32
AVCodecContext::extradata
uint8_t * extradata
Out-of-band global headers that may be used by some codecs.
Definition: avcodec.h:522
xvid_context::inter_matrix
unsigned char * inter_matrix
I-Frame Quant Matrix.
Definition: libxvid.c:79
internal.h
AVCodec::name
const char * name
Name of the codec implementation.
Definition: codec.h:179
AVCodecContext::height
int height
Definition: avcodec.h:600
AVCodecContext::pix_fmt
enum AVPixelFormat pix_fmt
Pixel format, see AV_PIX_FMT_xxx.
Definition: avcodec.h:639
AVCOL_RANGE_MPEG
@ AVCOL_RANGE_MPEG
Narrow or limited range content.
Definition: pixfmt.h:760
avcodec.h
xvid_ff_2pass_destroy
static int xvid_ff_2pass_destroy(struct xvid_context *ref, xvid_plg_destroy_t *param)
Destroy the two-pass plugin context.
Definition: libxvid.c:150
AV_CODEC_FLAG_CLOSED_GOP
#define AV_CODEC_FLAG_CLOSED_GOP
Definition: avcodec.h:332
ret
ret
Definition: filter_design.txt:187
VE
#define VE
Definition: libxvid.c:885
AVClass::class_name
const char * class_name
The name of the class; usually it is the same name as the context structure type to which the AVClass...
Definition: log.h:81
xvid_encode_init
static av_cold int xvid_encode_init(AVCodecContext *avctx)
Definition: libxvid.c:372
AV_RL32
uint64_t_TMPL AV_WL64 unsigned int_TMPL AV_RL32
Definition: bytestream.h:92
AVCodecContext
main external API structure.
Definition: avcodec.h:439
xvid_context::mpeg_quant
int mpeg_quant
Quantization type.
Definition: libxvid.c:86
AV_PICTURE_TYPE_B
@ AV_PICTURE_TYPE_B
Bi-dir predicted.
Definition: avutil.h:280
xvid_context::ssim
int ssim
SSIM information display mode.
Definition: libxvid.c:82
AVCodecContext::qmin
int qmin
minimum quantizer
Definition: avcodec.h:1246
AVRational::den
int den
Denominator.
Definition: rational.h:60
AV_OPT_TYPE_INT
@ AV_OPT_TYPE_INT
Underlying C type is int.
Definition: opt.h:259
ref
static int ref[MAX_W *MAX_W]
Definition: jpeg2000dwt.c:117
xvid_context::lumi_aq
int lumi_aq
Lumi masking as an aq method.
Definition: libxvid.c:80
xvid_context::twopassbuffer
char * twopassbuffer
Character buffer for two-pass.
Definition: libxvid.c:74
ff_libxvid_encoder
const FFCodec ff_libxvid_encoder
Definition: libxvid.c:907
FF_MB_DECISION_RD
#define FF_MB_DECISION_RD
rate distortion
Definition: avcodec.h:947
xvid_ff_pass1
Structure for the private first-pass plugin.
Definition: libxvid.c:92
AV_PICTURE_TYPE_P
@ AV_PICTURE_TYPE_P
Predicted.
Definition: avutil.h:279
AVMEDIA_TYPE_VIDEO
@ AVMEDIA_TYPE_VIDEO
Definition: avutil.h:200
mem.h
AVCodecContext::max_b_frames
int max_b_frames
maximum number of B-frames between non-B-frames Note: The output will be delayed by max_b_frames+1 re...
Definition: avcodec.h:777
av_free
#define av_free(p)
Definition: tableprint_vlc.h:34
AVCodecContext::codec_tag
unsigned int codec_tag
fourcc (LSB first, so "ABCD" -> ('D'<<24) + ('C'<<16) + ('B'<<8) + 'A').
Definition: avcodec.h:464
FF_MB_DECISION_BITS
#define FF_MB_DECISION_BITS
chooses the one which needs the fewest bits
Definition: avcodec.h:946
AVCodecContext::priv_data
void * priv_data
Definition: avcodec.h:466
AVPacket
This structure stores compressed data.
Definition: packet.h:572
av_freep
#define av_freep(p)
Definition: tableprint_vlc.h:35
AVCodecContext::inter_matrix
uint16_t * inter_matrix
custom inter quantization matrix Must be allocated with the av_malloc() family of functions,...
Definition: avcodec.h:965
xvid_context::ysize
int ysize
Frame y size.
Definition: libxvid.c:68
xvid_ff_2pass_create
static int xvid_ff_2pass_create(xvid_plg_create_t *param, void **handle)
Initialize the two-pass plugin and context.
Definition: libxvid.c:119
AVCodecContext::width
int width
picture width / height.
Definition: avcodec.h:600
AVFrame::linesize
int linesize[AV_NUM_DATA_POINTERS]
For video, a positive or negative value, which is typically indicating the size in bytes of each pict...
Definition: frame.h:480
av_log
#define av_log(a,...)
Definition: tableprint_vlc.h:27
xvid_ff_2pass_after
static int xvid_ff_2pass_after(struct xvid_context *ref, xvid_plg_data_t *param)
Capture statistic data and write it during first pass.
Definition: libxvid.c:211
xvid_correct_framerate
static void xvid_correct_framerate(AVCodecContext *avctx)
Routine to correct a possibly erroneous framerate being fed to us.
Definition: libxvid.c:322
pkt
static AVPacket * pkt
Definition: demux_decode.c:55
FF_QP2LAMBDA
#define FF_QP2LAMBDA
factor to convert from H.263 QP to lambda
Definition: avutil.h:226
xvid_context::gmc
int gmc
Definition: libxvid.c:84
AV_PICTURE_TYPE_S
@ AV_PICTURE_TYPE_S
S(GMC)-VOP MPEG-4.
Definition: avutil.h:281
AV_OPT_TYPE_CONST
@ AV_OPT_TYPE_CONST
Special option type for declaring named constants.
Definition: opt.h:299
snprintf
#define snprintf
Definition: snprintf.h:34
ff_alloc_packet
int ff_alloc_packet(AVCodecContext *avctx, AVPacket *avpkt, int64_t size)
Check AVPacket size and allocate data.
Definition: encode.c:61
AVCodecContext::sample_aspect_ratio
AVRational sample_aspect_ratio
sample aspect ratio (0 if unknown) That is the width of a pixel divided by the height of the pixel.
Definition: avcodec.h:624
AV_CODEC_FLAG_PASS1
#define AV_CODEC_FLAG_PASS1
Use internal 2pass ratecontrol in first pass mode.
Definition: avcodec.h:290
ff_check_codec_matrices
int ff_check_codec_matrices(AVCodecContext *avctx, unsigned types, uint16_t min, uint16_t max)
Definition: encode.c:944
FF_MATRIX_TYPE_INTER
#define FF_MATRIX_TYPE_INTER
Definition: encode.h:119
frame_types
static const int8_t frame_types[4]
Definition: rv60dec.c:38