You've already forked FFmpeg
mirror of
https://github.com/FFmpeg/FFmpeg.git
synced 2025-11-23 21:54:53 +02:00
fix obnoxious ogg_packet passing from encoder to muxer
Originally committed as revision 2955 to svn://svn.ffmpeg.org/ffmpeg/trunk
This commit is contained in:
@@ -9,13 +9,17 @@
|
|||||||
#include "avcodec.h"
|
#include "avcodec.h"
|
||||||
#include "oggvorbis.h"
|
#include "oggvorbis.h"
|
||||||
|
|
||||||
#define OGGVORBIS_FRAME_SIZE 1024
|
//#define OGGVORBIS_FRAME_SIZE 1024
|
||||||
|
#define OGGVORBIS_FRAME_SIZE 64
|
||||||
|
|
||||||
|
#define BUFFER_SIZE (1024*64)
|
||||||
|
|
||||||
typedef struct OggVorbisContext {
|
typedef struct OggVorbisContext {
|
||||||
vorbis_info vi ;
|
vorbis_info vi ;
|
||||||
vorbis_dsp_state vd ;
|
vorbis_dsp_state vd ;
|
||||||
vorbis_block vb ;
|
vorbis_block vb ;
|
||||||
|
uint8_t buffer[BUFFER_SIZE];
|
||||||
|
int buffer_index;
|
||||||
|
|
||||||
/* decoder */
|
/* decoder */
|
||||||
vorbis_comment vc ;
|
vorbis_comment vc ;
|
||||||
@@ -85,20 +89,33 @@ static int oggvorbis_encode_frame(AVCodecContext *avccontext,
|
|||||||
|
|
||||||
vorbis_analysis_wrote(&context->vd, samples) ;
|
vorbis_analysis_wrote(&context->vd, samples) ;
|
||||||
|
|
||||||
l = 0 ;
|
|
||||||
|
|
||||||
while(vorbis_analysis_blockout(&context->vd, &context->vb) == 1) {
|
while(vorbis_analysis_blockout(&context->vd, &context->vb) == 1) {
|
||||||
vorbis_analysis(&context->vb, NULL);
|
vorbis_analysis(&context->vb, NULL);
|
||||||
vorbis_bitrate_addblock(&context->vb) ;
|
vorbis_bitrate_addblock(&context->vb) ;
|
||||||
|
|
||||||
while(vorbis_bitrate_flushpacket(&context->vd, &op)) {
|
while(vorbis_bitrate_flushpacket(&context->vd, &op)) {
|
||||||
memcpy(packets + l, &op, sizeof(ogg_packet)) ;
|
memcpy(context->buffer + context->buffer_index, &op, sizeof(ogg_packet));
|
||||||
memcpy(packets + l + sizeof(ogg_packet), op.packet, op.bytes) ;
|
context->buffer_index += sizeof(ogg_packet);
|
||||||
l += sizeof(ogg_packet) + op.bytes ;
|
memcpy(context->buffer + context->buffer_index, op.packet, op.bytes);
|
||||||
|
context->buffer_index += op.bytes;
|
||||||
|
// av_log(avccontext, AV_LOG_DEBUG, "e%d / %d\n", context->buffer_index, op.bytes);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return l ;
|
if(context->buffer_index){
|
||||||
|
ogg_packet *op2= context->buffer;
|
||||||
|
op2->packet = context->buffer + sizeof(ogg_packet);
|
||||||
|
l= op2->bytes;
|
||||||
|
|
||||||
|
memcpy(packets, op2->packet, l);
|
||||||
|
context->buffer_index -= l + sizeof(ogg_packet);
|
||||||
|
memcpy(context->buffer, context->buffer + l + sizeof(ogg_packet), context->buffer_index);
|
||||||
|
|
||||||
|
// av_log(avccontext, AV_LOG_DEBUG, "E%d\n", l);
|
||||||
|
return l;
|
||||||
|
}
|
||||||
|
|
||||||
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@@ -14,6 +14,9 @@
|
|||||||
#include "avformat.h"
|
#include "avformat.h"
|
||||||
#include "oggvorbis.h"
|
#include "oggvorbis.h"
|
||||||
|
|
||||||
|
#undef NDEBUG
|
||||||
|
#include <assert.h>
|
||||||
|
|
||||||
#define DECODER_BUFFER_SIZE 4096
|
#define DECODER_BUFFER_SIZE 4096
|
||||||
|
|
||||||
|
|
||||||
@@ -21,8 +24,7 @@ typedef struct OggContext {
|
|||||||
/* output */
|
/* output */
|
||||||
ogg_stream_state os ;
|
ogg_stream_state os ;
|
||||||
int header_handled ;
|
int header_handled ;
|
||||||
ogg_int64_t base_packet_no ;
|
ogg_packet op;
|
||||||
ogg_int64_t base_granule_pos ;
|
|
||||||
|
|
||||||
/* input */
|
/* input */
|
||||||
ogg_sync_state oy ;
|
ogg_sync_state oy ;
|
||||||
@@ -41,6 +43,8 @@ static int ogg_write_header(AVFormatContext *avfcontext)
|
|||||||
ogg_packet header, header_comm, header_code ;
|
ogg_packet header, header_comm, header_code ;
|
||||||
int n ;
|
int n ;
|
||||||
|
|
||||||
|
av_set_pts_info(avfcontext, 60, 1, AV_TIME_BASE);
|
||||||
|
|
||||||
ogg_stream_init(&context->os, 31415);
|
ogg_stream_init(&context->os, 31415);
|
||||||
|
|
||||||
for(n = 0 ; n < avfcontext->nb_streams ; n++) {
|
for(n = 0 ; n < avfcontext->nb_streams ; n++) {
|
||||||
@@ -79,7 +83,6 @@ static int ogg_write_header(AVFormatContext *avfcontext)
|
|||||||
/* end of vorbis specific code */
|
/* end of vorbis specific code */
|
||||||
|
|
||||||
context->header_handled = 0 ;
|
context->header_handled = 0 ;
|
||||||
context->base_packet_no = 0 ;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return 0 ;
|
return 0 ;
|
||||||
@@ -88,12 +91,20 @@ static int ogg_write_header(AVFormatContext *avfcontext)
|
|||||||
|
|
||||||
static int ogg_write_packet(AVFormatContext *avfcontext,
|
static int ogg_write_packet(AVFormatContext *avfcontext,
|
||||||
int stream_index,
|
int stream_index,
|
||||||
const uint8_t *buf, int size, int64_t force_pts)
|
const uint8_t *buf, int size, int64_t pts)
|
||||||
{
|
{
|
||||||
OggContext *context = avfcontext->priv_data ;
|
OggContext *context = avfcontext->priv_data ;
|
||||||
ogg_packet *op ;
|
AVCodecContext *avctx= &avfcontext->streams[stream_index]->codec;
|
||||||
|
ogg_packet *op= &context->op;
|
||||||
ogg_page og ;
|
ogg_page og ;
|
||||||
int l = 0 ;
|
|
||||||
|
pts= av_rescale(pts, avctx->sample_rate, AV_TIME_BASE);
|
||||||
|
|
||||||
|
if(!size){
|
||||||
|
// av_log(avfcontext, AV_LOG_DEBUG, "zero packet\n");
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
// av_log(avfcontext, AV_LOG_DEBUG, "M%d\n", size);
|
||||||
|
|
||||||
/* flush header packets so audio starts on a new page */
|
/* flush header packets so audio starts on a new page */
|
||||||
|
|
||||||
@@ -106,29 +117,21 @@ static int ogg_write_packet(AVFormatContext *avfcontext,
|
|||||||
context->header_handled = 1 ;
|
context->header_handled = 1 ;
|
||||||
}
|
}
|
||||||
|
|
||||||
while(l < size) {
|
op->packet = (uint8_t*) buf;
|
||||||
op = (ogg_packet*)(buf + l) ;
|
op->bytes = size;
|
||||||
op->packet = (uint8_t*) buf + l + sizeof( ogg_packet) ; /* fix data pointer */
|
op->b_o_s = op->packetno == 0;
|
||||||
|
op->granulepos= pts;
|
||||||
|
|
||||||
if(!context->base_packet_no) { /* this is the first packet */
|
/* correct the fields in the packet -- essential for streaming */
|
||||||
context->base_packet_no = op->packetno ;
|
|
||||||
context->base_granule_pos = op->granulepos ;
|
|
||||||
}
|
|
||||||
|
|
||||||
/* correct the fields in the packet -- essential for streaming */
|
ogg_stream_packetin(&context->os, op);
|
||||||
|
|
||||||
op->packetno -= context->base_packet_no ;
|
while(ogg_stream_pageout(&context->os, &og)) {
|
||||||
op->granulepos -= context->base_granule_pos ;
|
put_buffer(&avfcontext->pb, og.header, og.header_len);
|
||||||
|
put_buffer(&avfcontext->pb, og.body, og.body_len);
|
||||||
ogg_stream_packetin(&context->os, op) ;
|
put_flush_packet(&avfcontext->pb);
|
||||||
l += sizeof(ogg_packet) + op->bytes ;
|
|
||||||
|
|
||||||
while(ogg_stream_pageout(&context->os, &og)) {
|
|
||||||
put_buffer(&avfcontext->pb, og.header, og.header_len) ;
|
|
||||||
put_buffer(&avfcontext->pb, og.body, og.body_len) ;
|
|
||||||
put_flush_packet(&avfcontext->pb);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
op->packetno++;
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1707,7 +1707,11 @@ int av_write_frame(AVFormatContext *s, int stream_index, const uint8_t *buf,
|
|||||||
switch (st->codec.codec_type) {
|
switch (st->codec.codec_type) {
|
||||||
case CODEC_TYPE_AUDIO:
|
case CODEC_TYPE_AUDIO:
|
||||||
frame_size = get_audio_frame_size(&st->codec, size);
|
frame_size = get_audio_frame_size(&st->codec, size);
|
||||||
if (frame_size >= 0) {
|
|
||||||
|
/* note, we skip the initial 0-size packets as they are most likely equal to the encoder delay,
|
||||||
|
but it would be better if we had the real timestamps from the encoder */
|
||||||
|
// av_log(s, AV_LOG_DEBUG, "%d %lld %lld\n", size, st->pts.num, st->pts.val);
|
||||||
|
if (frame_size >= 0 && (size || st->pts.num!=st->pts.den>>1 || st->pts.val)) {
|
||||||
av_frac_add(&st->pts,
|
av_frac_add(&st->pts,
|
||||||
(int64_t)s->pts_den * frame_size);
|
(int64_t)s->pts_den * frame_size);
|
||||||
}
|
}
|
||||||
@@ -1900,6 +1904,8 @@ int64_t parse_date(const char *datestr, int duration)
|
|||||||
const char *q;
|
const char *q;
|
||||||
int is_utc, len;
|
int is_utc, len;
|
||||||
char lastch;
|
char lastch;
|
||||||
|
|
||||||
|
#undef time
|
||||||
time_t now = time(0);
|
time_t now = time(0);
|
||||||
|
|
||||||
len = strlen(datestr);
|
len = strlen(datestr);
|
||||||
|
|||||||
Reference in New Issue
Block a user