comparison libxvidff.c @ 6908:25dcd46b8160 libavcodec

cosmetics: XviD is now called Xvid.
author diego
date Tue, 27 May 2008 12:32:59 +0000
parents e1302edb0f69
children 130784da5595
comparison
equal deleted inserted replaced
6907:8b0db8b07a21 6908:25dcd46b8160
39 39
40 /* For PPC Use */ 40 /* For PPC Use */
41 extern int has_altivec(void); 41 extern int has_altivec(void);
42 42
43 /** 43 /**
44 * Structure for the private XviD context. 44 * Structure for the private Xvid context.
45 * This stores all the private context for the codec. 45 * This stores all the private context for the codec.
46 */ 46 */
47 typedef struct xvid_context { 47 typedef struct xvid_context {
48 void *encoder_handle; /** Handle for XviD Encoder */ 48 void *encoder_handle; /** Handle for Xvid encoder */
49 int xsize, ysize; /** Frame size */ 49 int xsize, ysize; /** Frame size */
50 int vop_flags; /** VOP flags for XviD Encoder */ 50 int vop_flags; /** VOP flags for Xvid encoder */
51 int vol_flags; /** VOL flags for XviD Encoder */ 51 int vol_flags; /** VOL flags for Xvid encoder */
52 int me_flags; /** Motion Estimation flags */ 52 int me_flags; /** Motion Estimation flags */
53 int qscale; /** Do we use constant scale? */ 53 int qscale; /** Do we use constant scale? */
54 int quicktime_format; /** Are we in a QT-based format? */ 54 int quicktime_format; /** Are we in a QT-based format? */
55 AVFrame encoded_picture; /** Encoded frame information */ 55 AVFrame encoded_picture; /** Encoded frame information */
56 char *twopassbuffer; /** Character buffer for two-pass */ 56 char *twopassbuffer; /** Character buffer for two-pass */
62 62
63 /** 63 /**
64 * Structure for the private first-pass plugin. 64 * Structure for the private first-pass plugin.
65 */ 65 */
66 typedef struct xvid_ff_pass1 { 66 typedef struct xvid_ff_pass1 {
67 int version; /** XviD version */ 67 int version; /** Xvid version */
68 xvid_context_t *context; /** Pointer to private context */ 68 xvid_context_t *context; /** Pointer to private context */
69 } xvid_ff_pass1_t; 69 } xvid_ff_pass1_t;
70 70
71 /* Prototypes - See function implementation for details */ 71 /* Prototypes - See function implementation for details */
72 int xvid_strip_vol_header(AVCodecContext *avctx, unsigned char *frame, unsigned int header_len, unsigned int frame_len); 72 int xvid_strip_vol_header(AVCodecContext *avctx, unsigned char *frame, unsigned int header_len, unsigned int frame_len);
164 memset(&xvid_gbl_init, 0, sizeof(xvid_gbl_init)); 164 memset(&xvid_gbl_init, 0, sizeof(xvid_gbl_init));
165 xvid_gbl_init.version = XVID_VERSION; 165 xvid_gbl_init.version = XVID_VERSION;
166 xvid_gbl_init.debug = 0; 166 xvid_gbl_init.debug = 0;
167 167
168 #ifdef ARCH_POWERPC 168 #ifdef ARCH_POWERPC
169 /* XviD's PPC support is borked, use libavcodec to detect */ 169 /* Xvid's PPC support is borked, use libavcodec to detect */
170 #ifdef HAVE_ALTIVEC 170 #ifdef HAVE_ALTIVEC
171 if( has_altivec() ) { 171 if( has_altivec() ) {
172 xvid_gbl_init.cpu_flags = XVID_CPU_FORCE | XVID_CPU_ALTIVEC; 172 xvid_gbl_init.cpu_flags = XVID_CPU_FORCE | XVID_CPU_ALTIVEC;
173 } else 173 } else
174 #endif 174 #endif
175 xvid_gbl_init.cpu_flags = XVID_CPU_FORCE; 175 xvid_gbl_init.cpu_flags = XVID_CPU_FORCE;
176 #else 176 #else
177 /* XviD can detect on x86 */ 177 /* Xvid can detect on x86 */
178 xvid_gbl_init.cpu_flags = 0; 178 xvid_gbl_init.cpu_flags = 0;
179 #endif 179 #endif
180 180
181 /* Initialize */ 181 /* Initialize */
182 xvid_global(NULL, XVID_GBL_INIT, &xvid_gbl_init, NULL); 182 xvid_global(NULL, XVID_GBL_INIT, &xvid_gbl_init, NULL);
187 187
188 /* Store the desired frame size */ 188 /* Store the desired frame size */
189 xvid_enc_create.width = x->xsize = avctx->width; 189 xvid_enc_create.width = x->xsize = avctx->width;
190 xvid_enc_create.height = x->ysize = avctx->height; 190 xvid_enc_create.height = x->ysize = avctx->height;
191 191
192 /* XviD can determine the proper profile to use */ 192 /* Xvid can determine the proper profile to use */
193 /* xvid_enc_create.profile = XVID_PROFILE_S_L3; */ 193 /* xvid_enc_create.profile = XVID_PROFILE_S_L3; */
194 194
195 /* We don't use zones */ 195 /* We don't use zones */
196 xvid_enc_create.zones = NULL; 196 xvid_enc_create.zones = NULL;
197 xvid_enc_create.num_zones = 0; 197 xvid_enc_create.num_zones = 0;
212 rc2pass1.context = x; 212 rc2pass1.context = x;
213 x->twopassbuffer = av_malloc(BUFFER_SIZE); 213 x->twopassbuffer = av_malloc(BUFFER_SIZE);
214 x->old_twopassbuffer = av_malloc(BUFFER_SIZE); 214 x->old_twopassbuffer = av_malloc(BUFFER_SIZE);
215 if( x->twopassbuffer == NULL || x->old_twopassbuffer == NULL ) { 215 if( x->twopassbuffer == NULL || x->old_twopassbuffer == NULL ) {
216 av_log(avctx, AV_LOG_ERROR, 216 av_log(avctx, AV_LOG_ERROR,
217 "XviD: Cannot allocate 2-pass log buffers\n"); 217 "Xvid: Cannot allocate 2-pass log buffers\n");
218 return -1; 218 return -1;
219 } 219 }
220 x->twopassbuffer[0] = x->old_twopassbuffer[0] = 0; 220 x->twopassbuffer[0] = x->old_twopassbuffer[0] = 0;
221 221
222 plugins[xvid_enc_create.num_plugins].func = xvid_ff_2pass; 222 plugins[xvid_enc_create.num_plugins].func = xvid_ff_2pass;
228 rc2pass2.bitrate = avctx->bit_rate; 228 rc2pass2.bitrate = avctx->bit_rate;
229 229
230 fd = av_tempfile("xvidff.", &(x->twopassfile)); 230 fd = av_tempfile("xvidff.", &(x->twopassfile));
231 if( fd == -1 ) { 231 if( fd == -1 ) {
232 av_log(avctx, AV_LOG_ERROR, 232 av_log(avctx, AV_LOG_ERROR,
233 "XviD: Cannot write 2-pass pipe\n"); 233 "Xvid: Cannot write 2-pass pipe\n");
234 return -1; 234 return -1;
235 } 235 }
236 236
237 if( avctx->stats_in == NULL ) { 237 if( avctx->stats_in == NULL ) {
238 av_log(avctx, AV_LOG_ERROR, 238 av_log(avctx, AV_LOG_ERROR,
239 "XviD: No 2-pass information loaded for second pass\n"); 239 "Xvid: No 2-pass information loaded for second pass\n");
240 return -1; 240 return -1;
241 } 241 }
242 242
243 if( strlen(avctx->stats_in) > 243 if( strlen(avctx->stats_in) >
244 write(fd, avctx->stats_in, strlen(avctx->stats_in)) ) { 244 write(fd, avctx->stats_in, strlen(avctx->stats_in)) ) {
245 close(fd); 245 close(fd);
246 av_log(avctx, AV_LOG_ERROR, 246 av_log(avctx, AV_LOG_ERROR,
247 "XviD: Cannot write to 2-pass pipe\n"); 247 "Xvid: Cannot write to 2-pass pipe\n");
248 return -1; 248 return -1;
249 } 249 }
250 250
251 close(fd); 251 close(fd);
252 rc2pass2.filename = x->twopassfile; 252 rc2pass2.filename = x->twopassfile;
276 xvid_enc_create.fincr = avctx->time_base.num; 276 xvid_enc_create.fincr = avctx->time_base.num;
277 xvid_enc_create.fbase = avctx->time_base.den; 277 xvid_enc_create.fbase = avctx->time_base.den;
278 if( avctx->gop_size > 0 ) 278 if( avctx->gop_size > 0 )
279 xvid_enc_create.max_key_interval = avctx->gop_size; 279 xvid_enc_create.max_key_interval = avctx->gop_size;
280 else 280 else
281 xvid_enc_create.max_key_interval = 240; /* XviD's best default */ 281 xvid_enc_create.max_key_interval = 240; /* Xvid's best default */
282 282
283 /* Quants */ 283 /* Quants */
284 if( xvid_flags & CODEC_FLAG_QSCALE ) x->qscale = 1; 284 if( xvid_flags & CODEC_FLAG_QSCALE ) x->qscale = 1;
285 else x->qscale = 0; 285 else x->qscale = 0;
286 286
329 if( xvid_flags & CODEC_FLAG_GLOBAL_HEADER ) { 329 if( xvid_flags & CODEC_FLAG_GLOBAL_HEADER ) {
330 /* In this case, we are claiming to be MPEG4 */ 330 /* In this case, we are claiming to be MPEG4 */
331 x->quicktime_format = 1; 331 x->quicktime_format = 1;
332 avctx->codec_id = CODEC_ID_MPEG4; 332 avctx->codec_id = CODEC_ID_MPEG4;
333 } else { 333 } else {
334 /* We are claiming to be XviD */ 334 /* We are claiming to be Xvid */
335 x->quicktime_format = 0; 335 x->quicktime_format = 0;
336 if(!avctx->codec_tag) 336 if(!avctx->codec_tag)
337 avctx->codec_tag = ff_get_fourcc("xvid"); 337 avctx->codec_tag = ff_get_fourcc("xvid");
338 } 338 }
339 339
344 if( avctx->max_b_frames > 0 && !x->quicktime_format ) xvid_enc_create.global |= XVID_GLOBAL_PACKED; 344 if( avctx->max_b_frames > 0 && !x->quicktime_format ) xvid_enc_create.global |= XVID_GLOBAL_PACKED;
345 345
346 /* Create encoder context */ 346 /* Create encoder context */
347 xerr = xvid_encore(NULL, XVID_ENC_CREATE, &xvid_enc_create, NULL); 347 xerr = xvid_encore(NULL, XVID_ENC_CREATE, &xvid_enc_create, NULL);
348 if( xerr ) { 348 if( xerr ) {
349 av_log(avctx, AV_LOG_ERROR, "XviD: Could not create encoder reference\n"); 349 av_log(avctx, AV_LOG_ERROR, "Xvid: Could not create encoder reference\n");
350 return -1; 350 return -1;
351 } 351 }
352 352
353 x->encoder_handle = xvid_enc_create.handle; 353 x->encoder_handle = xvid_enc_create.handle;
354 avctx->coded_frame = &x->encoded_picture; 354 avctx->coded_frame = &x->encoded_picture;
381 xvid_enc_frame.version = XVID_VERSION; 381 xvid_enc_frame.version = XVID_VERSION;
382 memset(&xvid_enc_stats, 0, sizeof(xvid_enc_stats)); 382 memset(&xvid_enc_stats, 0, sizeof(xvid_enc_stats));
383 xvid_enc_stats.version = XVID_VERSION; 383 xvid_enc_stats.version = XVID_VERSION;
384 *p = *picture; 384 *p = *picture;
385 385
386 /* Let XviD know where to put the frame. */ 386 /* Let Xvid know where to put the frame. */
387 xvid_enc_frame.bitstream = frame; 387 xvid_enc_frame.bitstream = frame;
388 xvid_enc_frame.length = buf_size; 388 xvid_enc_frame.length = buf_size;
389 389
390 /* Initialize input image fields */ 390 /* Initialize input image fields */
391 if( avctx->pix_fmt != PIX_FMT_YUV420P ) { 391 if( avctx->pix_fmt != PIX_FMT_YUV420P ) {
392 av_log(avctx, AV_LOG_ERROR, "XviD: Color spaces other than 420p not supported\n"); 392 av_log(avctx, AV_LOG_ERROR, "Xvid: Color spaces other than 420p not supported\n");
393 return -1; 393 return -1;
394 } 394 }
395 395
396 xvid_enc_frame.input.csp = XVID_CSP_PLANAR; /* YUV420P */ 396 xvid_enc_frame.input.csp = XVID_CSP_PLANAR; /* YUV420P */
397 397
459 } else 459 } else
460 p->key_frame = 0; 460 p->key_frame = 0;
461 461
462 return xerr; 462 return xerr;
463 } else { 463 } else {
464 av_log(avctx, AV_LOG_ERROR, "XviD: Encoding Error Occurred: %i\n", xerr); 464 av_log(avctx, AV_LOG_ERROR, "Xvid: Encoding Error Occurred: %i\n", xerr);
465 return -1; 465 return -1;
466 } 466 }
467 } 467 }
468 468
469 /** 469 /**
470 * Destroys the private context for the encoder. 470 * Destroys the private context for the encoder.
471 * All buffers are freed, and the XviD encoder context is destroyed. 471 * All buffers are freed, and the Xvid encoder context is destroyed.
472 * 472 *
473 * @param avctx AVCodecContext pointer to context 473 * @param avctx AVCodecContext pointer to context
474 * @return Returns 0, success guaranteed 474 * @return Returns 0, success guaranteed
475 */ 475 */
476 av_cold int ff_xvid_encode_close(AVCodecContext *avctx) { 476 av_cold int ff_xvid_encode_close(AVCodecContext *avctx) {
494 return 0; 494 return 0;
495 } 495 }
496 496
497 /** 497 /**
498 * Routine to create a global VO/VOL header for MP4 container. 498 * Routine to create a global VO/VOL header for MP4 container.
499 * What we do here is extract the header from the XviD bitstream 499 * What we do here is extract the header from the Xvid bitstream
500 * as it is encoded. We also strip the repeated headers from the 500 * as it is encoded. We also strip the repeated headers from the
501 * bitstream when a global header is requested for MPEG-4 ISO 501 * bitstream when a global header is requested for MPEG-4 ISO
502 * compliance. 502 * compliance.
503 * 503 *
504 * @param avctx AVCodecContext pointer to context 504 * @param avctx AVCodecContext pointer to context
538 return frame_len; 538 return frame_len;
539 } 539 }
540 540
541 /** 541 /**
542 * Routine to correct a possibly erroneous framerate being fed to us. 542 * Routine to correct a possibly erroneous framerate being fed to us.
543 * XviD currently chokes on framerates where the ticks per frame is 543 * Xvid currently chokes on framerates where the ticks per frame is
544 * extremely large. This function works to correct problems in this area 544 * extremely large. This function works to correct problems in this area
545 * by estimating a new framerate and taking the simpler fraction of 545 * by estimating a new framerate and taking the simpler fraction of
546 * the two presented. 546 * the two presented.
547 * 547 *
548 * @param avctx Context that contains the framerate to correct. 548 * @param avctx Context that contains the framerate to correct.
586 586
587 if( fbase > est_fbase ) { 587 if( fbase > est_fbase ) {
588 avctx->time_base.den = est_frate; 588 avctx->time_base.den = est_frate;
589 avctx->time_base.num = est_fbase; 589 avctx->time_base.num = est_fbase;
590 av_log(avctx, AV_LOG_DEBUG, 590 av_log(avctx, AV_LOG_DEBUG,
591 "XviD: framerate re-estimated: %.2f, %.3f%% correction\n", 591 "Xvid: framerate re-estimated: %.2f, %.3f%% correction\n",
592 est_fps, (((est_fps - fps)/fps) * 100.0)); 592 est_fps, (((est_fps - fps)/fps) * 100.0));
593 } else { 593 } else {
594 avctx->time_base.den = frate; 594 avctx->time_base.den = frate;
595 avctx->time_base.num = fbase; 595 avctx->time_base.num = fbase;
596 } 596 }
597 } 597 }
598 598
599 /* 599 /*
600 * XviD 2-Pass Kludge Section 600 * Xvid 2-Pass Kludge Section
601 * 601 *
602 * XviD's default 2-pass doesn't allow us to create data as we need to, so 602 * Xvid's default 2-pass doesn't allow us to create data as we need to, so
603 * this section spends time replacing the first pass plugin so we can write 603 * this section spends time replacing the first pass plugin so we can write
604 * statistic information as libavcodec requests in. We have another kludge 604 * statistic information as libavcodec requests in. We have another kludge
605 * that allows us to pass data to the second pass in XviD without a custom 605 * that allows us to pass data to the second pass in Xvid without a custom
606 * rate-control plugin. 606 * rate-control plugin.
607 */ 607 */
608 608
609 /** 609 /**
610 * Initializes the two-pass plugin and context. 610 * Initializes the two-pass plugin and context.
728 return 0; 728 return 0;
729 } 729 }
730 730
731 /** 731 /**
732 * Dispatch function for our custom plugin. 732 * Dispatch function for our custom plugin.
733 * This handles the dispatch for the XviD plugin. It passes data 733 * This handles the dispatch for the Xvid plugin. It passes data
734 * on to other functions for actual processing. 734 * on to other functions for actual processing.
735 * 735 *
736 * @param ref Context pointer for the plugin 736 * @param ref Context pointer for the plugin
737 * @param cmd The task given for us to complete 737 * @param cmd The task given for us to complete
738 * @param p1 First parameter (varies) 738 * @param p1 First parameter (varies)
761 return XVID_ERR_FAIL; 761 return XVID_ERR_FAIL;
762 } 762 }
763 } 763 }
764 764
765 /** 765 /**
766 * XviD codec definition for libavcodec. 766 * Xvid codec definition for libavcodec.
767 */ 767 */
768 AVCodec libxvid_encoder = { 768 AVCodec libxvid_encoder = {
769 "libxvid", 769 "libxvid",
770 CODEC_TYPE_VIDEO, 770 CODEC_TYPE_VIDEO,
771 CODEC_ID_XVID, 771 CODEC_ID_XVID,