|
|
|
@ -38,14 +38,16 @@ |
|
|
|
|
/* libtheora includes */ |
|
|
|
|
#include <theora/theora.h> |
|
|
|
|
|
|
|
|
|
typedef struct TheoraContext{ |
|
|
|
|
typedef struct TheoraContext { |
|
|
|
|
theora_state t_state; |
|
|
|
|
} TheoraContext; |
|
|
|
|
|
|
|
|
|
/*!
|
|
|
|
|
Concatenates an ogg_packet into the extradata. |
|
|
|
|
*/ |
|
|
|
|
static int concatenate_packet(unsigned int* offset, AVCodecContext* avc_context, const ogg_packet* packet) |
|
|
|
|
static int concatenate_packet(unsigned int* offset, |
|
|
|
|
AVCodecContext* avc_context, |
|
|
|
|
const ogg_packet* packet) |
|
|
|
|
{ |
|
|
|
|
const char* message = NULL; |
|
|
|
|
uint8_t* newdata = NULL; |
|
|
|
@ -71,7 +73,7 @@ static int concatenate_packet(unsigned int* offset, AVCodecContext* avc_context, |
|
|
|
|
avc_context->extradata_size = newsize; |
|
|
|
|
AV_WB16(avc_context->extradata + (*offset), packet->bytes); |
|
|
|
|
*offset += 2; |
|
|
|
|
memcpy( avc_context->extradata + (*offset), packet->packet, packet->bytes ); |
|
|
|
|
memcpy(avc_context->extradata + (*offset), packet->packet, packet->bytes); |
|
|
|
|
(*offset) += packet->bytes; |
|
|
|
|
return 0; |
|
|
|
|
} |
|
|
|
@ -85,7 +87,7 @@ static av_cold int encode_init(AVCodecContext* avc_context) |
|
|
|
|
TheoraContext *h = avc_context->priv_data; |
|
|
|
|
|
|
|
|
|
/* Set up the theora_info struct */ |
|
|
|
|
theora_info_init( &t_info ); |
|
|
|
|
theora_info_init(&t_info); |
|
|
|
|
t_info.width = FFALIGN(avc_context->width, 16); |
|
|
|
|
t_info.height = FFALIGN(avc_context->height, 16); |
|
|
|
|
t_info.frame_width = avc_context->width; |
|
|
|
@ -131,13 +133,13 @@ static av_cold int encode_init(AVCodecContext* avc_context) |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
/* Now initialise libtheora */ |
|
|
|
|
if (theora_encode_init( &(h->t_state), &t_info ) != 0) { |
|
|
|
|
if (theora_encode_init(&(h->t_state), &t_info) != 0) { |
|
|
|
|
av_log(avc_context, AV_LOG_ERROR, "theora_encode_init failed\n"); |
|
|
|
|
return -1; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
/* Clear up theora_info struct */ |
|
|
|
|
theora_info_clear( &t_info ); |
|
|
|
|
theora_info_clear(&t_info); |
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
Output first header packet consisting of theora |
|
|
|
@ -149,24 +151,24 @@ static av_cold int encode_init(AVCodecContext* avc_context) |
|
|
|
|
offset = 0; |
|
|
|
|
|
|
|
|
|
/* Header */ |
|
|
|
|
theora_encode_header( &(h->t_state), &o_packet ); |
|
|
|
|
if (concatenate_packet( &offset, avc_context, &o_packet ) != 0) |
|
|
|
|
theora_encode_header(&(h->t_state), &o_packet); |
|
|
|
|
if (concatenate_packet(&offset, avc_context, &o_packet) != 0) |
|
|
|
|
return -1; |
|
|
|
|
|
|
|
|
|
/* Comment */ |
|
|
|
|
theora_comment_init( &t_comment ); |
|
|
|
|
theora_encode_comment( &t_comment, &o_packet ); |
|
|
|
|
if (concatenate_packet( &offset, avc_context, &o_packet ) != 0) |
|
|
|
|
theora_comment_init(&t_comment); |
|
|
|
|
theora_encode_comment(&t_comment, &o_packet); |
|
|
|
|
if (concatenate_packet(&offset, avc_context, &o_packet) != 0) |
|
|
|
|
return -1; |
|
|
|
|
/* Clear up theora_comment struct before we reset the packet */ |
|
|
|
|
theora_comment_clear( &t_comment ); |
|
|
|
|
theora_comment_clear(&t_comment); |
|
|
|
|
/* And despite documentation to the contrary, theora_comment_clear
|
|
|
|
|
* does not release the packet */ |
|
|
|
|
ogg_packet_clear(&o_packet); |
|
|
|
|
|
|
|
|
|
/* Tables */ |
|
|
|
|
theora_encode_tables( &(h->t_state), &o_packet ); |
|
|
|
|
if (concatenate_packet( &offset, avc_context, &o_packet ) != 0) |
|
|
|
|
theora_encode_tables(&(h->t_state), &o_packet); |
|
|
|
|
if (concatenate_packet(&offset, avc_context, &o_packet) != 0) |
|
|
|
|
return -1; |
|
|
|
|
|
|
|
|
|
/* Set up the output AVFrame */ |
|
|
|
@ -175,11 +177,8 @@ static av_cold int encode_init(AVCodecContext* avc_context) |
|
|
|
|
return 0; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
static int encode_frame( |
|
|
|
|
AVCodecContext* avc_context, |
|
|
|
|
uint8_t *outbuf, |
|
|
|
|
int buf_size, |
|
|
|
|
void *data) |
|
|
|
|
static int encode_frame(AVCodecContext* avc_context, uint8_t *outbuf, |
|
|
|
|
int buf_size, void *data) |
|
|
|
|
{ |
|
|
|
|
yuv_buffer t_yuv_buffer; |
|
|
|
|
TheoraContext *h = avc_context->priv_data; |
|
|
|
@ -207,7 +206,7 @@ static int encode_frame( |
|
|
|
|
t_yuv_buffer.v = frame->data[2]; |
|
|
|
|
|
|
|
|
|
/* Now call into theora_encode_YUVin */ |
|
|
|
|
result = theora_encode_YUVin( &(h->t_state), &t_yuv_buffer ); |
|
|
|
|
result = theora_encode_YUVin(&(h->t_state), &t_yuv_buffer); |
|
|
|
|
if (result != 0) { |
|
|
|
|
const char* message; |
|
|
|
|
switch (result) { |
|
|
|
@ -226,7 +225,7 @@ static int encode_frame( |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
/* Pick up returned ogg_packet */ |
|
|
|
|
result = theora_encode_packetout( &(h->t_state), 0, &o_packet ); |
|
|
|
|
result = theora_encode_packetout(&(h->t_state), 0, &o_packet); |
|
|
|
|
switch (result) { |
|
|
|
|
case 0: |
|
|
|
|
/* No packet is ready */ |
|
|
|
@ -247,7 +246,7 @@ static int encode_frame( |
|
|
|
|
memcpy(outbuf, o_packet.packet, o_packet.bytes); |
|
|
|
|
|
|
|
|
|
// HACK: does not take codec delay into account (neither does the decoder though)
|
|
|
|
|
avc_context->coded_frame->pts= frame->pts; |
|
|
|
|
avc_context->coded_frame->pts = frame->pts; |
|
|
|
|
|
|
|
|
|
return o_packet.bytes; |
|
|
|
|
} |
|
|
|
@ -259,15 +258,15 @@ static av_cold int encode_close(AVCodecContext* avc_context) |
|
|
|
|
int result; |
|
|
|
|
const char* message; |
|
|
|
|
|
|
|
|
|
result = theora_encode_packetout( &(h->t_state), 1, &o_packet ); |
|
|
|
|
theora_clear( &(h->t_state) ); |
|
|
|
|
result = theora_encode_packetout(&(h->t_state), 1, &o_packet); |
|
|
|
|
theora_clear(&(h->t_state)); |
|
|
|
|
av_freep(&avc_context->coded_frame); |
|
|
|
|
av_freep(&avc_context->extradata); |
|
|
|
|
avc_context->extradata_size = 0; |
|
|
|
|
|
|
|
|
|
switch (result) { |
|
|
|
|
case 0:/* No packet is ready */ |
|
|
|
|
case -1:/* Encoding finished */ |
|
|
|
|
case 0: /* No packet is ready */ |
|
|
|
|
case -1: /* Encoding finished */ |
|
|
|
|
return 0; |
|
|
|
|
case 1: |
|
|
|
|
/* We have a packet */ |
|
|
|
@ -284,8 +283,7 @@ static av_cold int encode_close(AVCodecContext* avc_context) |
|
|
|
|
static const enum PixelFormat supported_pixel_formats[] = { PIX_FMT_YUV420P, PIX_FMT_NONE }; |
|
|
|
|
|
|
|
|
|
/*! AVCodec struct exposed to libavcodec */ |
|
|
|
|
AVCodec libtheora_encoder = |
|
|
|
|
{ |
|
|
|
|
AVCodec libtheora_encoder = { |
|
|
|
|
.name = "libtheora", |
|
|
|
|
.type = CODEC_TYPE_VIDEO, |
|
|
|
|
.id = CODEC_ID_THEORA, |
|
|
|
|