Skip to content

Commit

Permalink
Use dynamically allocated ByteIOContext in AVFormatContext
Browse files Browse the repository at this point in the history
patch by: Björn Axelsson, bjorn d axelsson a intinor d se
thread: [PATCH] Remove static ByteIOContexts, 06 nov 2007



git-svn-id: file:///var/local/repositories/ffmpeg/trunk@11071 9553f0bf-9b14-0410-a0b8-cfaf0461ba5b
  • Loading branch information
andoma committed Nov 21, 2007
1 parent 315194a commit 9ce92bc
Show file tree
Hide file tree
Showing 89 changed files with 874 additions and 825 deletions.
8 changes: 4 additions & 4 deletions ffmpeg.c
Expand Up @@ -920,9 +920,9 @@ static void print_report(AVFormatContext **output_files,


oc = output_files[0]; oc = output_files[0];


total_size = url_fsize(&oc->pb); total_size = url_fsize(oc->pb);
if(total_size<0) // FIXME improve url_fsize() so it works with non seekable output too if(total_size<0) // FIXME improve url_fsize() so it works with non seekable output too
total_size= url_ftell(&oc->pb); total_size= url_ftell(oc->pb);


buf[0] = '\0'; buf[0] = '\0';
ti1 = 1e10; ti1 = 1e10;
Expand Down Expand Up @@ -1948,7 +1948,7 @@ static int av_encode(AVFormatContext **output_files,
break; break;


/* finish if limit size exhausted */ /* finish if limit size exhausted */
if (limit_filesize != 0 && limit_filesize < url_ftell(&output_files[0]->pb)) if (limit_filesize != 0 && limit_filesize < url_ftell(output_files[0]->pb))
break; break;


/* read a frame from it and output it in the fifo */ /* read a frame from it and output it in the fifo */
Expand Down Expand Up @@ -3835,7 +3835,7 @@ static int av_exit()
AVFormatContext *s = output_files[i]; AVFormatContext *s = output_files[i];
int j; int j;
if (!(s->oformat->flags & AVFMT_NOFILE)) if (!(s->oformat->flags & AVFMT_NOFILE))
url_fclose(&s->pb); url_fclose(s->pb);
for(j=0;j<s->nb_streams;j++) { for(j=0;j<s->nb_streams;j++) {
av_free(s->streams[j]->codec); av_free(s->streams[j]->codec);
av_free(s->streams[j]); av_free(s->streams[j]);
Expand Down
11 changes: 6 additions & 5 deletions ffplay.c
Expand Up @@ -1912,7 +1912,8 @@ static int decode_thread(void *arg)
ret = -1; ret = -1;
goto fail; goto fail;
} }
ic->pb.eof_reached= 0; //FIXME hack, ffplay maybe should not use url_feof() to test for the end if(ic->pb)
ic->pb->eof_reached= 0; //FIXME hack, ffplay maybe should not use url_feof() to test for the end


/* if seeking requested, we execute it */ /* if seeking requested, we execute it */
if (start_time != AV_NOPTS_VALUE) { if (start_time != AV_NOPTS_VALUE) {
Expand Down Expand Up @@ -1980,7 +1981,7 @@ static int decode_thread(void *arg)
#if defined(CONFIG_RTSP_DEMUXER) || defined(CONFIG_MMSH_PROTOCOL) #if defined(CONFIG_RTSP_DEMUXER) || defined(CONFIG_MMSH_PROTOCOL)
if (is->paused && if (is->paused &&
(!strcmp(ic->iformat->name, "rtsp") || (!strcmp(ic->iformat->name, "rtsp") ||
!strcmp(url_fileno(&ic->pb)->prot->name, "mmsh"))) { (ic->pb && !strcmp(url_fileno(ic->pb)->prot->name, "mmsh")))) {
/* wait 10 ms to avoid trying to get another packet */ /* wait 10 ms to avoid trying to get another packet */
/* XXX: horrible */ /* XXX: horrible */
SDL_Delay(10); SDL_Delay(10);
Expand Down Expand Up @@ -2023,14 +2024,14 @@ static int decode_thread(void *arg)
if (is->audioq.size > MAX_AUDIOQ_SIZE || if (is->audioq.size > MAX_AUDIOQ_SIZE ||
is->videoq.size > MAX_VIDEOQ_SIZE || is->videoq.size > MAX_VIDEOQ_SIZE ||
is->subtitleq.size > MAX_SUBTITLEQ_SIZE || is->subtitleq.size > MAX_SUBTITLEQ_SIZE ||
url_feof(&ic->pb)) { url_feof(ic->pb)) {
/* wait 10 ms */ /* wait 10 ms */
SDL_Delay(10); SDL_Delay(10);
continue; continue;
} }
ret = av_read_frame(ic, pkt); ret = av_read_frame(ic, pkt);
if (ret < 0) { if (ret < 0) {
if (url_ferror(&ic->pb) == 0) { if (url_ferror(ic->pb) == 0) {
SDL_Delay(100); /* wait for user event */ SDL_Delay(100); /* wait for user event */
continue; continue;
} else } else
Expand Down Expand Up @@ -2282,7 +2283,7 @@ static void event_loop(void)
do_seek: do_seek:
if (cur_stream) { if (cur_stream) {
if (seek_by_bytes) { if (seek_by_bytes) {
pos = url_ftell(&cur_stream->ic->pb); pos = url_ftell(cur_stream->ic->pb);
if (cur_stream->ic->bit_rate) if (cur_stream->ic->bit_rate)
incr *= cur_stream->ic->bit_rate / 60.0; incr *= cur_stream->ic->bit_rate / 60.0;
else else
Expand Down
33 changes: 15 additions & 18 deletions ffserver.c
Expand Up @@ -752,7 +752,7 @@ static void close_connection(HTTPContext *c)
/* prepare header */ /* prepare header */
if (url_open_dyn_buf(&ctx->pb) >= 0) { if (url_open_dyn_buf(&ctx->pb) >= 0) {
av_write_trailer(ctx); av_write_trailer(ctx);
url_close_dyn_buf(&ctx->pb, &c->pb_buffer); url_close_dyn_buf(ctx->pb, &c->pb_buffer);
} }
} }
} }
Expand Down Expand Up @@ -1596,9 +1596,9 @@ static void compute_stats(HTTPContext *c)
char *p; char *p;
time_t ti; time_t ti;
int i, len; int i, len;
ByteIOContext pb1, *pb = &pb1; ByteIOContext *pb;


if (url_open_dyn_buf(pb) < 0) { if (url_open_dyn_buf(&pb) < 0) {
/* XXX: return an error ? */ /* XXX: return an error ? */
c->buffer_ptr = c->buffer; c->buffer_ptr = c->buffer;
c->buffer_end = c->buffer; c->buffer_end = c->buffer;
Expand Down Expand Up @@ -2032,13 +2032,13 @@ static int http_prepare_data(HTTPContext *c)
/* XXX: potential leak */ /* XXX: potential leak */
return -1; return -1;
} }
c->fmt_ctx.pb.is_streamed = 1; c->fmt_ctx.pb->is_streamed = 1;


av_set_parameters(&c->fmt_ctx, NULL); av_set_parameters(&c->fmt_ctx, NULL);
if (av_write_header(&c->fmt_ctx) < 0) if (av_write_header(&c->fmt_ctx) < 0)
return -1; return -1;


len = url_close_dyn_buf(&c->fmt_ctx.pb, &c->pb_buffer); len = url_close_dyn_buf(c->fmt_ctx.pb, &c->pb_buffer);
c->buffer_ptr = c->pb_buffer; c->buffer_ptr = c->pb_buffer;
c->buffer_end = c->pb_buffer + len; c->buffer_end = c->pb_buffer + len;


Expand Down Expand Up @@ -2182,7 +2182,7 @@ static int http_prepare_data(HTTPContext *c)
if (av_write_frame(ctx, &pkt)) if (av_write_frame(ctx, &pkt))
c->state = HTTPSTATE_SEND_DATA_TRAILER; c->state = HTTPSTATE_SEND_DATA_TRAILER;


len = url_close_dyn_buf(&ctx->pb, &c->pb_buffer); len = url_close_dyn_buf(ctx->pb, &c->pb_buffer);
c->cur_frame_bytes = len; c->cur_frame_bytes = len;
c->buffer_ptr = c->pb_buffer; c->buffer_ptr = c->pb_buffer;
c->buffer_end = c->pb_buffer + len; c->buffer_end = c->pb_buffer + len;
Expand All @@ -2208,7 +2208,7 @@ static int http_prepare_data(HTTPContext *c)
return -1; return -1;
} }
av_write_trailer(ctx); av_write_trailer(ctx);
len = url_close_dyn_buf(&ctx->pb, &c->pb_buffer); len = url_close_dyn_buf(ctx->pb, &c->pb_buffer);
c->buffer_ptr = c->pb_buffer; c->buffer_ptr = c->pb_buffer;
c->buffer_end = c->pb_buffer + len; c->buffer_end = c->pb_buffer + len;


Expand Down Expand Up @@ -2261,7 +2261,7 @@ static int http_send_data(HTTPContext *c)


if (c->rtp_protocol == RTSP_PROTOCOL_RTP_TCP) { if (c->rtp_protocol == RTSP_PROTOCOL_RTP_TCP) {
/* RTP packets are sent inside the RTSP TCP connection */ /* RTP packets are sent inside the RTSP TCP connection */
ByteIOContext pb1, *pb = &pb1; ByteIOContext *pb;
int interleaved_index, size; int interleaved_index, size;
uint8_t header[4]; uint8_t header[4];
HTTPContext *rtsp_c; HTTPContext *rtsp_c;
Expand All @@ -2273,7 +2273,7 @@ static int http_send_data(HTTPContext *c)
/* if already sending something, then wait. */ /* if already sending something, then wait. */
if (rtsp_c->state != RTSPSTATE_WAIT_REQUEST) if (rtsp_c->state != RTSPSTATE_WAIT_REQUEST)
break; break;
if (url_open_dyn_buf(pb) < 0) if (url_open_dyn_buf(&pb) < 0)
goto fail1; goto fail1;
interleaved_index = c->packet_stream_index * 2; interleaved_index = c->packet_stream_index * 2;
/* RTCP packets are sent at odd indexes */ /* RTCP packets are sent at odd indexes */
Expand Down Expand Up @@ -2432,14 +2432,13 @@ static int http_receive_data(HTTPContext *c)
/* We have a header in our hands that contains useful data */ /* We have a header in our hands that contains useful data */
AVFormatContext s; AVFormatContext s;
AVInputFormat *fmt_in; AVInputFormat *fmt_in;
ByteIOContext *pb = &s.pb;
int i; int i;


memset(&s, 0, sizeof(s)); memset(&s, 0, sizeof(s));


url_open_buf(pb, c->buffer, c->buffer_end - c->buffer, URL_RDONLY); url_open_buf(&s.pb, c->buffer, c->buffer_end - c->buffer, URL_RDONLY);
pb->buf_end = c->buffer_end; /* ?? */ s.pb->buf_end = c->buffer_end; /* ?? */
pb->is_streamed = 1; s.pb->is_streamed = 1;


/* use feed output format name to find corresponding input format */ /* use feed output format name to find corresponding input format */
fmt_in = av_find_input_format(feed->fmt->name); fmt_in = av_find_input_format(feed->fmt->name);
Expand Down Expand Up @@ -2553,7 +2552,6 @@ static int rtsp_parse_request(HTTPContext *c)
char url[1024]; char url[1024];
char protocol[32]; char protocol[32];
char line[1024]; char line[1024];
ByteIOContext pb1;
int len; int len;
RTSPHeader header1, *header = &header1; RTSPHeader header1, *header = &header1;


Expand All @@ -2568,8 +2566,7 @@ static int rtsp_parse_request(HTTPContext *c)
av_strlcpy(c->url, url, sizeof(c->url)); av_strlcpy(c->url, url, sizeof(c->url));
av_strlcpy(c->protocol, protocol, sizeof(c->protocol)); av_strlcpy(c->protocol, protocol, sizeof(c->protocol));


c->pb = &pb1; if (url_open_dyn_buf(&c->pb) < 0) {
if (url_open_dyn_buf(c->pb) < 0) {
/* XXX: cannot do more */ /* XXX: cannot do more */
c->pb = NULL; /* safety */ c->pb = NULL; /* safety */
return -1; return -1;
Expand Down Expand Up @@ -3164,7 +3161,7 @@ static int rtp_new_av_stream(HTTPContext *c,
av_free(ctx); av_free(ctx);
return -1; return -1;
} }
url_close_dyn_buf(&ctx->pb, &dummy_buf); url_close_dyn_buf(ctx->pb, &dummy_buf);
av_free(dummy_buf); av_free(dummy_buf);


c->rtp_ctx[stream_index] = ctx; c->rtp_ctx[stream_index] = ctx;
Expand Down Expand Up @@ -3479,7 +3476,7 @@ static void build_feed_streams(void)
} }
/* XXX: need better api */ /* XXX: need better api */
av_freep(&s->priv_data); av_freep(&s->priv_data);
url_fclose(&s->pb); url_fclose(s->pb);
} }
/* get feed size and write index */ /* get feed size and write index */
fd = open(feed->feed_filename, O_RDONLY); fd = open(feed->feed_filename, O_RDONLY);
Expand Down
12 changes: 6 additions & 6 deletions libavformat/4xm.c
Expand Up @@ -92,7 +92,7 @@ static int fourxm_probe(AVProbeData *p)
static int fourxm_read_header(AVFormatContext *s, static int fourxm_read_header(AVFormatContext *s,
AVFormatParameters *ap) AVFormatParameters *ap)
{ {
ByteIOContext *pb = &s->pb; ByteIOContext *pb = s->pb;
unsigned int fourcc_tag; unsigned int fourcc_tag;
unsigned int size; unsigned int size;
int header_size; int header_size;
Expand Down Expand Up @@ -224,7 +224,7 @@ static int fourxm_read_packet(AVFormatContext *s,
AVPacket *pkt) AVPacket *pkt)
{ {
FourxmDemuxContext *fourxm = s->priv_data; FourxmDemuxContext *fourxm = s->priv_data;
ByteIOContext *pb = &s->pb; ByteIOContext *pb = s->pb;
unsigned int fourcc_tag; unsigned int fourcc_tag;
unsigned int size, out_size; unsigned int size, out_size;
int ret = 0; int ret = 0;
Expand All @@ -235,7 +235,7 @@ static int fourxm_read_packet(AVFormatContext *s,


while (!packet_read) { while (!packet_read) {


if ((ret = get_buffer(&s->pb, header, 8)) < 0) if ((ret = get_buffer(s->pb, header, 8)) < 0)
return ret; return ret;
fourcc_tag = AV_RL32(&header[0]); fourcc_tag = AV_RL32(&header[0]);
size = AV_RL32(&header[4]); size = AV_RL32(&header[4]);
Expand Down Expand Up @@ -265,9 +265,9 @@ static int fourxm_read_packet(AVFormatContext *s,
return AVERROR(EIO); return AVERROR(EIO);
pkt->stream_index = fourxm->video_stream_index; pkt->stream_index = fourxm->video_stream_index;
pkt->pts = fourxm->video_pts; pkt->pts = fourxm->video_pts;
pkt->pos = url_ftell(&s->pb); pkt->pos = url_ftell(s->pb);
memcpy(pkt->data, header, 8); memcpy(pkt->data, header, 8);
ret = get_buffer(&s->pb, &pkt->data[8], size); ret = get_buffer(s->pb, &pkt->data[8], size);


if (ret < 0) if (ret < 0)
av_free_packet(pkt); av_free_packet(pkt);
Expand All @@ -282,7 +282,7 @@ static int fourxm_read_packet(AVFormatContext *s,
size-=8; size-=8;


if (track_number == fourxm->selected_track) { if (track_number == fourxm->selected_track) {
ret= av_get_packet(&s->pb, pkt, size); ret= av_get_packet(s->pb, pkt, size);
if(ret<0) if(ret<0)
return AVERROR(EIO); return AVERROR(EIO);
pkt->stream_index = pkt->stream_index =
Expand Down
4 changes: 2 additions & 2 deletions libavformat/adtsenc.c
Expand Up @@ -84,15 +84,15 @@ static int adts_write_frame_header(AVFormatContext *s, int size)
put_bits(&pb, 2, 0); /* number_of_raw_data_blocks_in_frame */ put_bits(&pb, 2, 0); /* number_of_raw_data_blocks_in_frame */


flush_put_bits(&pb); flush_put_bits(&pb);
put_buffer(&s->pb, buf, ADTS_HEADER_SIZE); put_buffer(s->pb, buf, ADTS_HEADER_SIZE);


return 0; return 0;
} }


static int adts_write_packet(AVFormatContext *s, AVPacket *pkt) static int adts_write_packet(AVFormatContext *s, AVPacket *pkt)
{ {
ADTSContext *adts = s->priv_data; ADTSContext *adts = s->priv_data;
ByteIOContext *pb = &s->pb; ByteIOContext *pb = s->pb;


if (!pkt->size) if (!pkt->size)
return 0; return 0;
Expand Down
14 changes: 7 additions & 7 deletions libavformat/aiff.c
Expand Up @@ -161,7 +161,7 @@ typedef struct {
static int aiff_write_header(AVFormatContext *s) static int aiff_write_header(AVFormatContext *s)
{ {
AIFFOutputContext *aiff = s->priv_data; AIFFOutputContext *aiff = s->priv_data;
ByteIOContext *pb = &s->pb; ByteIOContext *pb = s->pb;
AVCodecContext *enc = s->streams[0]->codec; AVCodecContext *enc = s->streams[0]->codec;
AVExtFloat sample_rate; AVExtFloat sample_rate;
int aifc = 0; int aifc = 0;
Expand Down Expand Up @@ -231,14 +231,14 @@ static int aiff_write_header(AVFormatContext *s)


static int aiff_write_packet(AVFormatContext *s, AVPacket *pkt) static int aiff_write_packet(AVFormatContext *s, AVPacket *pkt)
{ {
ByteIOContext *pb = &s->pb; ByteIOContext *pb = s->pb;
put_buffer(pb, pkt->data, pkt->size); put_buffer(pb, pkt->data, pkt->size);
return 0; return 0;
} }


static int aiff_write_trailer(AVFormatContext *s) static int aiff_write_trailer(AVFormatContext *s)
{ {
ByteIOContext *pb = &s->pb; ByteIOContext *pb = s->pb;
AIFFOutputContext *aiff = s->priv_data; AIFFOutputContext *aiff = s->priv_data;
AVCodecContext *enc = s->streams[0]->codec; AVCodecContext *enc = s->streams[0]->codec;


Expand All @@ -250,7 +250,7 @@ static int aiff_write_trailer(AVFormatContext *s)
end_size++; end_size++;
} }


if (!url_is_streamed(&s->pb)) { if (!url_is_streamed(s->pb)) {
/* File length */ /* File length */
url_fseek(pb, aiff->form, SEEK_SET); url_fseek(pb, aiff->form, SEEK_SET);
put_be32(pb, (uint32_t)(file_size - aiff->form - 4)); put_be32(pb, (uint32_t)(file_size - aiff->form - 4));
Expand Down Expand Up @@ -293,7 +293,7 @@ static int aiff_read_header(AVFormatContext *s,
offset_t offset = 0; offset_t offset = 0;
uint32_t tag; uint32_t tag;
unsigned version = AIFF_C_VERSION1; unsigned version = AIFF_C_VERSION1;
ByteIOContext *pb = &s->pb; ByteIOContext *pb = s->pb;
AVStream * st = s->streams[0]; AVStream * st = s->streams[0];


/* check FORM header */ /* check FORM header */
Expand Down Expand Up @@ -399,11 +399,11 @@ static int aiff_read_packet(AVFormatContext *s,
int res; int res;


/* End of stream may be reached */ /* End of stream may be reached */
if (url_feof(&s->pb)) if (url_feof(s->pb))
return AVERROR(EIO); return AVERROR(EIO);


/* Now for that packet */ /* Now for that packet */
res = av_get_packet(&s->pb, pkt, (MAX_SIZE / st->codec->block_align) * st->codec->block_align); res = av_get_packet(s->pb, pkt, (MAX_SIZE / st->codec->block_align) * st->codec->block_align);
if (res < 0) if (res < 0)
return res; return res;


Expand Down
16 changes: 8 additions & 8 deletions libavformat/amr.c
Expand Up @@ -33,7 +33,7 @@ static const char AMRWB_header [] = "#!AMR-WB\n";
#ifdef CONFIG_MUXERS #ifdef CONFIG_MUXERS
static int amr_write_header(AVFormatContext *s) static int amr_write_header(AVFormatContext *s)
{ {
ByteIOContext *pb = &s->pb; ByteIOContext *pb = s->pb;
AVCodecContext *enc = s->streams[0]->codec; AVCodecContext *enc = s->streams[0]->codec;


s->priv_data = NULL; s->priv_data = NULL;
Expand All @@ -56,8 +56,8 @@ static int amr_write_header(AVFormatContext *s)


static int amr_write_packet(AVFormatContext *s, AVPacket *pkt) static int amr_write_packet(AVFormatContext *s, AVPacket *pkt)
{ {
put_buffer(&s->pb, pkt->data, pkt->size); put_buffer(s->pb, pkt->data, pkt->size);
put_flush_packet(&s->pb); put_flush_packet(s->pb);
return 0; return 0;
} }
#endif /* CONFIG_MUXERS */ #endif /* CONFIG_MUXERS */
Expand All @@ -78,7 +78,7 @@ static int amr_probe(AVProbeData *p)
static int amr_read_header(AVFormatContext *s, static int amr_read_header(AVFormatContext *s,
AVFormatParameters *ap) AVFormatParameters *ap)
{ {
ByteIOContext *pb = &s->pb; ByteIOContext *pb = s->pb;
AVStream *st; AVStream *st;
uint8_t header[9]; uint8_t header[9];


Expand Down Expand Up @@ -120,13 +120,13 @@ static int amr_read_packet(AVFormatContext *s,
AVCodecContext *enc = s->streams[0]->codec; AVCodecContext *enc = s->streams[0]->codec;
int read, size = 0, toc, mode; int read, size = 0, toc, mode;


if (url_feof(&s->pb)) if (url_feof(s->pb))
{ {
return AVERROR(EIO); return AVERROR(EIO);
} }


//FIXME this is wrong, this should rather be in a AVParset //FIXME this is wrong, this should rather be in a AVParset
toc=get_byte(&s->pb); toc=get_byte(s->pb);
mode = (toc >> 3) & 0x0F; mode = (toc >> 3) & 0x0F;


if (enc->codec_id == CODEC_ID_AMR_NB) if (enc->codec_id == CODEC_ID_AMR_NB)
Expand All @@ -152,10 +152,10 @@ static int amr_read_packet(AVFormatContext *s,
} }


pkt->stream_index = 0; pkt->stream_index = 0;
pkt->pos= url_ftell(&s->pb); pkt->pos= url_ftell(s->pb);
pkt->data[0]=toc; pkt->data[0]=toc;
pkt->duration= enc->codec_id == CODEC_ID_AMR_NB ? 160 : 320; pkt->duration= enc->codec_id == CODEC_ID_AMR_NB ? 160 : 320;
read = get_buffer(&s->pb, pkt->data+1, size-1); read = get_buffer(s->pb, pkt->data+1, size-1);


if (read != size-1) if (read != size-1)
{ {
Expand Down

0 comments on commit 9ce92bc

Please sign in to comment.