comparison options.c @ 11565:2baae9246958 libavcodec

Add avcodec_copy_context().
author rbultje
date Wed, 31 Mar 2010 20:40:49 +0000
parents 8a4984c5cacc
children 6bc036ad8ff9
comparison
equal deleted inserted replaced
11564:a9780299ef48 11565:2baae9246958
469 469
470 AVCodecContext *avcodec_alloc_context(void){ 470 AVCodecContext *avcodec_alloc_context(void){
471 return avcodec_alloc_context2(AVMEDIA_TYPE_UNKNOWN); 471 return avcodec_alloc_context2(AVMEDIA_TYPE_UNKNOWN);
472 } 472 }
473 473
474 int avcodec_copy_context(AVCodecContext *dest, const AVCodecContext *src)
475 {
476 if (dest->codec) { // check that the dest context is uninitialized
477 av_log(dest, AV_LOG_ERROR,
478 "Tried to copy AVCodecContext %p into already-initialized %p\n",
479 src, dest);
480 return AVERROR(EINVAL);
481 }
482 memcpy(dest, src, sizeof(*dest));
483
484 /* set values specific to opened codecs back to their default state */
485 dest->priv_data = NULL;
486 dest->codec = NULL;
487 dest->palctrl = NULL;
488 dest->slice_offset = NULL;
489 dest->internal_buffer = NULL;
490 dest->hwaccel = NULL;
491 dest->execute = NULL;
492 dest->execute2 = NULL;
493 dest->reget_buffer = NULL;
494 dest->thread_opaque = NULL;
495
496 /* reallocate values that should be allocated separately */
497 dest->rc_eq = NULL;
498 dest->extradata = NULL;
499 dest->intra_matrix = NULL;
500 dest->inter_matrix = NULL;
501 dest->rc_override = NULL;
502 if (src->rc_eq) {
503 dest->rc_eq = av_strdup(src->rc_eq);
504 if (!dest->rc_eq)
505 return AVERROR(ENOMEM);
506 }
507
508 #define alloc_and_copy_or_fail(obj, size, pad) \
509 if (src->obj && size > 0) { \
510 dest->obj = av_malloc(size + pad); \
511 if (!dest->obj) \
512 goto fail; \
513 memcpy(dest->obj, src->obj, size); \
514 if (pad) \
515 memset(((uint8_t *) dest->obj) + size, 0, pad); \
516 }
517 alloc_and_copy_or_fail(extradata, src->extradata_size,
518 FF_INPUT_BUFFER_PADDING_SIZE);
519 alloc_and_copy_or_fail(intra_matrix, 64 * sizeof(int16_t), 0);
520 alloc_and_copy_or_fail(inter_matrix, 64 * sizeof(int16_t), 0);
521 alloc_and_copy_or_fail(rc_override, src->rc_override_count * sizeof(*src->rc_override), 0);
522 #undef alloc_and_copy_or_fail
523
524 return 0;
525
526 fail:
527 av_freep(&dest->rc_override);
528 av_freep(&dest->intra_matrix);
529 av_freep(&dest->inter_matrix);
530 av_freep(&dest->extradata);
531 av_freep(&dest->rc_eq);
532 return AVERROR(ENOMEM);
533 }