|
|
|
@ -59,21 +59,26 @@ static int concatenate_packet(unsigned int* offset, |
|
|
|
|
const char* message = NULL; |
|
|
|
|
uint8_t* newdata = NULL; |
|
|
|
|
int newsize = avc_context->extradata_size + 2 + packet->bytes; |
|
|
|
|
int ret; |
|
|
|
|
|
|
|
|
|
if (packet->bytes < 0) { |
|
|
|
|
message = "ogg_packet has negative size"; |
|
|
|
|
ret = AVERROR_INVALIDDATA; |
|
|
|
|
} else if (packet->bytes > 0xffff) { |
|
|
|
|
message = "ogg_packet is larger than 65535 bytes"; |
|
|
|
|
ret = AVERROR_INVALIDDATA; |
|
|
|
|
} else if (newsize < avc_context->extradata_size) { |
|
|
|
|
message = "extradata_size would overflow"; |
|
|
|
|
ret = AVERROR_INVALIDDATA; |
|
|
|
|
} else { |
|
|
|
|
newdata = av_realloc(avc_context->extradata, newsize); |
|
|
|
|
if (!newdata) |
|
|
|
|
message = "av_realloc failed"; |
|
|
|
|
ret = AVERROR(ENOMEM); |
|
|
|
|
} |
|
|
|
|
if (message) { |
|
|
|
|
av_log(avc_context, AV_LOG_ERROR, "concatenate_packet failed: %s\n", message); |
|
|
|
|
return -1; |
|
|
|
|
return ret; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
avc_context->extradata = newdata; |
|
|
|
@ -95,7 +100,7 @@ static int get_stats(AVCodecContext *avctx, int eos) |
|
|
|
|
bytes = th_encode_ctl(h->t_state, TH_ENCCTL_2PASS_OUT, &buf, sizeof(buf)); |
|
|
|
|
if (bytes < 0) { |
|
|
|
|
av_log(avctx, AV_LOG_ERROR, "Error getting first pass stats\n"); |
|
|
|
|
return -1; |
|
|
|
|
return AVERROR_EXTERNAL; |
|
|
|
|
} |
|
|
|
|
if (!eos) { |
|
|
|
|
h->stats = av_fast_realloc(h->stats, &h->stats_size, |
|
|
|
@ -112,7 +117,7 @@ static int get_stats(AVCodecContext *avctx, int eos) |
|
|
|
|
return 0; |
|
|
|
|
#else |
|
|
|
|
av_log(avctx, AV_LOG_ERROR, "libtheora too old to support 2pass\n"); |
|
|
|
|
return -1; |
|
|
|
|
return AVERROR(ENOSUP); |
|
|
|
|
#endif |
|
|
|
|
} |
|
|
|
|
|
|
|
|
@ -126,7 +131,7 @@ static int submit_stats(AVCodecContext *avctx) |
|
|
|
|
if (!h->stats) { |
|
|
|
|
if (!avctx->stats_in) { |
|
|
|
|
av_log(avctx, AV_LOG_ERROR, "No statsfile for second pass\n"); |
|
|
|
|
return -1; |
|
|
|
|
return AVERROR(EINVAL); |
|
|
|
|
} |
|
|
|
|
h->stats_size = strlen(avctx->stats_in) * 3/4; |
|
|
|
|
h->stats = av_malloc(h->stats_size); |
|
|
|
@ -138,7 +143,7 @@ static int submit_stats(AVCodecContext *avctx) |
|
|
|
|
h->stats_size - h->stats_offset); |
|
|
|
|
if (bytes < 0) { |
|
|
|
|
av_log(avctx, AV_LOG_ERROR, "Error submitting stats\n"); |
|
|
|
|
return -1; |
|
|
|
|
return AVERROR_EXTERNAL; |
|
|
|
|
} |
|
|
|
|
if (!bytes) |
|
|
|
|
return 0; |
|
|
|
@ -147,7 +152,7 @@ static int submit_stats(AVCodecContext *avctx) |
|
|
|
|
return 0; |
|
|
|
|
#else |
|
|
|
|
av_log(avctx, AV_LOG_ERROR, "libtheora too old to support 2pass\n"); |
|
|
|
|
return -1; |
|
|
|
|
return AVERROR(ENOSUP); |
|
|
|
|
#endif |
|
|
|
|
} |
|
|
|
|
|
|
|
|
@ -159,6 +164,7 @@ static av_cold int encode_init(AVCodecContext* avc_context) |
|
|
|
|
unsigned int offset; |
|
|
|
|
TheoraContext *h = avc_context->priv_data; |
|
|
|
|
uint32_t gop_size = avc_context->gop_size; |
|
|
|
|
int ret; |
|
|
|
|
|
|
|
|
|
/* Set up the theora_info struct */ |
|
|
|
|
th_info_init(&t_info); |
|
|
|
@ -195,7 +201,7 @@ static av_cold int encode_init(AVCodecContext* avc_context) |
|
|
|
|
t_info.pixel_fmt = TH_PF_444; |
|
|
|
|
else { |
|
|
|
|
av_log(avc_context, AV_LOG_ERROR, "Unsupported pix_fmt\n"); |
|
|
|
|
return -1; |
|
|
|
|
return AVERROR(EINVAL); |
|
|
|
|
} |
|
|
|
|
avcodec_get_chroma_sub_sample(avc_context->pix_fmt, &h->uv_hshift, &h->uv_vshift); |
|
|
|
|
|
|
|
|
@ -216,7 +222,7 @@ static av_cold int encode_init(AVCodecContext* avc_context) |
|
|
|
|
h->t_state = th_encode_alloc(&t_info); |
|
|
|
|
if (!h->t_state) { |
|
|
|
|
av_log(avc_context, AV_LOG_ERROR, "theora_encode_init failed\n"); |
|
|
|
|
return -1; |
|
|
|
|
return AVERROR_EXTERNAL; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
h->keyframe_mask = (1 << t_info.keyframe_granule_shift) - 1; |
|
|
|
@ -226,16 +232,16 @@ static av_cold int encode_init(AVCodecContext* avc_context) |
|
|
|
|
if (th_encode_ctl(h->t_state, TH_ENCCTL_SET_KEYFRAME_FREQUENCY_FORCE, |
|
|
|
|
&gop_size, sizeof(gop_size))) { |
|
|
|
|
av_log(avc_context, AV_LOG_ERROR, "Error setting GOP size\n"); |
|
|
|
|
return -1; |
|
|
|
|
return AVERROR_EXTERNAL; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
// need to enable 2 pass (via TH_ENCCTL_2PASS_) before encoding headers
|
|
|
|
|
if (avc_context->flags & CODEC_FLAG_PASS1) { |
|
|
|
|
if (get_stats(avc_context, 0)) |
|
|
|
|
return -1; |
|
|
|
|
if ((ret = get_stats(avc_context, 0)) < 0) |
|
|
|
|
return ret; |
|
|
|
|
} else if (avc_context->flags & CODEC_FLAG_PASS2) { |
|
|
|
|
if (submit_stats(avc_context)) |
|
|
|
|
return -1; |
|
|
|
|
if ((ret = submit_stats(avc_context)) < 0) |
|
|
|
|
return ret; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
@ -251,8 +257,8 @@ static av_cold int encode_init(AVCodecContext* avc_context) |
|
|
|
|
th_comment_init(&t_comment); |
|
|
|
|
|
|
|
|
|
while (th_encode_flushheader(h->t_state, &t_comment, &o_packet)) |
|
|
|
|
if (concatenate_packet(&offset, avc_context, &o_packet)) |
|
|
|
|
return -1; |
|
|
|
|
if ((ret = concatenate_packet(&offset, avc_context, &o_packet)) < 0) |
|
|
|
|
return ret; |
|
|
|
|
|
|
|
|
|
th_comment_clear(&t_comment); |
|
|
|
|
|
|
|
|
@ -274,8 +280,8 @@ static int encode_frame(AVCodecContext* avc_context, AVPacket *pkt, |
|
|
|
|
if (!frame) { |
|
|
|
|
th_encode_packetout(h->t_state, 1, &o_packet); |
|
|
|
|
if (avc_context->flags & CODEC_FLAG_PASS1) |
|
|
|
|
if (get_stats(avc_context, 1)) |
|
|
|
|
return -1; |
|
|
|
|
if ((ret = get_stats(avc_context, 1)) < 0) |
|
|
|
|
return ret; |
|
|
|
|
return 0; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
@ -288,8 +294,8 @@ static int encode_frame(AVCodecContext* avc_context, AVPacket *pkt, |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
if (avc_context->flags & CODEC_FLAG_PASS2) |
|
|
|
|
if (submit_stats(avc_context)) |
|
|
|
|
return -1; |
|
|
|
|
if ((ret = submit_stats(avc_context)) < 0) |
|
|
|
|
return ret; |
|
|
|
|
|
|
|
|
|
/* Now call into theora_encode_YUVin */ |
|
|
|
|
result = th_encode_ycbcr_in(h->t_state, t_yuv_buffer); |
|
|
|
@ -307,12 +313,12 @@ static int encode_frame(AVCodecContext* avc_context, AVPacket *pkt, |
|
|
|
|
break; |
|
|
|
|
} |
|
|
|
|
av_log(avc_context, AV_LOG_ERROR, "theora_encode_YUVin failed (%s) [%d]\n", message, result); |
|
|
|
|
return -1; |
|
|
|
|
return AVERROR_EXTERNAL; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
if (avc_context->flags & CODEC_FLAG_PASS1) |
|
|
|
|
if (get_stats(avc_context, 0)) |
|
|
|
|
return -1; |
|
|
|
|
if ((ret = get_stats(avc_context, 0)) < 0) |
|
|
|
|
return ret; |
|
|
|
|
|
|
|
|
|
/* Pick up returned ogg_packet */ |
|
|
|
|
result = th_encode_packetout(h->t_state, 0, &o_packet); |
|
|
|
@ -325,7 +331,7 @@ static int encode_frame(AVCodecContext* avc_context, AVPacket *pkt, |
|
|
|
|
break; |
|
|
|
|
default: |
|
|
|
|
av_log(avc_context, AV_LOG_ERROR, "theora_encode_packetout failed [%d]\n", result); |
|
|
|
|
return -1; |
|
|
|
|
return AVERROR_EXTERNAL; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
/* Copy ogg_packet content out to buffer */ |
|
|
|
|