| 472 | } |
| 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->thread_opaque = NULL; |
| 492 | |
| 493 | /* reallocate values that should be allocated separately */ |
| 494 | dest->rc_eq = NULL; |
| 495 | dest->extradata = NULL; |
| 496 | dest->intra_matrix = NULL; |
| 497 | dest->inter_matrix = NULL; |
| 498 | dest->rc_override = NULL; |
| 499 | if (src->rc_eq) { |
| 500 | dest->rc_eq = av_strdup(src->rc_eq); |
| 501 | if (!dest->rc_eq) |
| 502 | return AVERROR(ENOMEM); |
| 503 | } |
| 504 | |
| 505 | #define alloc_and_copy_or_fail(obj, size, pad) \ |
| 506 | if (src->obj && size > 0) { \ |
| 507 | dest->obj = av_malloc(size + pad); \ |
| 508 | if (!dest->obj) \ |
| 509 | goto fail; \ |
| 510 | memcpy(dest->obj, src->obj, size); \ |
| 511 | if (pad) \ |
| 512 | memset(((uint8_t *) dest->obj) + size, 0, pad); \ |
| 513 | } |
| 514 | alloc_and_copy_or_fail(extradata, src->extradata_size, |
| 515 | FF_INPUT_BUFFER_PADDING_SIZE); |
| 516 | alloc_and_copy_or_fail(intra_matrix, 64 * sizeof(int16_t), 0); |
| 517 | alloc_and_copy_or_fail(inter_matrix, 64 * sizeof(int16_t), 0); |
| 518 | alloc_and_copy_or_fail(rc_override, src->rc_override_count * sizeof(*src->rc_override), 0); |
| 519 | #undef alloc_and_copy_or_fail |
| 520 | |
| 521 | return 0; |
| 522 | |
| 523 | fail: |
| 524 | av_freep(&dest->rc_override); |
| 525 | av_freep(&dest->intra_matrix); |
| 526 | av_freep(&dest->inter_matrix); |
| 527 | av_freep(&dest->extradata); |
| 528 | av_freep(&dest->rc_eq); |
| 529 | return AVERROR(ENOMEM); |
| 530 | } |
no test coverage detected