Skip to content

Commit

Permalink
lavf: replace AVStream.codec with AVStream.codecpar
Browse files Browse the repository at this point in the history
Currently, AVStream contains an embedded AVCodecContext instance, which
is used by demuxers to export stream parameters to the caller and by
muxers to receive stream parameters from the caller. It is also used
internally as the codec context that is passed to parsers.

In addition, it is also widely used by the callers as the decoding (when
demuxer) or encoding (when muxing) context, though this has been
officially discouraged since Libav 11.

There are multiple important problems with this approach:
    - the fields in AVCodecContext are in general one of
        * stream parameters
        * codec options
        * codec state
      However, it's not clear which ones are which. It is consequently
      unclear which fields are a demuxer allowed to set or a muxer allowed to
      read. This leads to erratic behaviour depending on whether decoding or
      encoding is being performed or not (and whether it uses the AVStream
      embedded codec context).
    - various synchronization issues arising from the fact that the same
      context is used by several different APIs (muxers/demuxers,
      parsers, bitstream filters and encoders/decoders) simultaneously, with
      there being no clear rules for who can modify what and the different
      processes being typically delayed with respect to each other.
    - avformat_find_stream_info() making it necessary to support opening
      and closing a single codec context multiple times, thus
      complicating the semantics of freeing various allocated objects in the
      codec context.

Those problems are resolved by replacing the AVStream embedded codec
context with a newly added AVCodecParameters instance, which stores only
the stream parameters exported by the demuxers or read by the muxers.
  • Loading branch information
elenril committed Feb 23, 2016
1 parent a806834 commit 9200514
Show file tree
Hide file tree
Showing 251 changed files with 4,515 additions and 4,304 deletions.
2 changes: 1 addition & 1 deletion libavdevice/alsa.c
Original file line number Diff line number Diff line change
Expand Up @@ -194,7 +194,7 @@ av_cold int ff_alsa_open(AVFormatContext *ctx, snd_pcm_stream_t mode,
snd_pcm_t *h;
snd_pcm_hw_params_t *hw_params;
snd_pcm_uframes_t buffer_size, period_size;
uint64_t layout = ctx->streams[0]->codec->channel_layout;
uint64_t layout = ctx->streams[0]->codecpar->channel_layout;

if (ctx->filename[0] == 0) audio_device = "default";
else audio_device = ctx->filename;
Expand Down
14 changes: 7 additions & 7 deletions libavdevice/alsa_dec.c
Original file line number Diff line number Diff line change
Expand Up @@ -101,10 +101,10 @@ static av_cold int audio_read_header(AVFormatContext *s1)
}

/* take real parameters */
st->codec->codec_type = AVMEDIA_TYPE_AUDIO;
st->codec->codec_id = codec_id;
st->codec->sample_rate = s->sample_rate;
st->codec->channels = s->channels;
st->codecpar->codec_type = AVMEDIA_TYPE_AUDIO;
st->codecpar->codec_id = codec_id;
st->codecpar->sample_rate = s->sample_rate;
st->codecpar->channels = s->channels;
avpriv_set_pts_info(st, 64, 1, 1000000); /* 64 bits pts in us */

return 0;
Expand Down Expand Up @@ -144,9 +144,9 @@ static int audio_read_packet(AVFormatContext *s1, AVPacket *pkt)
snd_pcm_htimestamp(s->h, &ts_delay, &timestamp);
ts_delay += res;
pkt->pts = timestamp.tv_sec * 1000000LL
+ (timestamp.tv_nsec * st->codec->sample_rate
- (int64_t)ts_delay * 1000000000LL + st->codec->sample_rate * 500LL)
/ (st->codec->sample_rate * 1000LL);
+ (timestamp.tv_nsec * st->codecpar->sample_rate
- (int64_t)ts_delay * 1000000000LL + st->codecpar->sample_rate * 500LL)
/ (st->codecpar->sample_rate * 1000LL);

pkt->size = res * s->frame_size;

Expand Down
10 changes: 5 additions & 5 deletions libavdevice/alsa_enc.c
Original file line number Diff line number Diff line change
Expand Up @@ -54,14 +54,14 @@ static av_cold int audio_write_header(AVFormatContext *s1)
int res;

st = s1->streams[0];
sample_rate = st->codec->sample_rate;
codec_id = st->codec->codec_id;
sample_rate = st->codecpar->sample_rate;
codec_id = st->codecpar->codec_id;
res = ff_alsa_open(s1, SND_PCM_STREAM_PLAYBACK, &sample_rate,
st->codec->channels, &codec_id);
if (sample_rate != st->codec->sample_rate) {
st->codecpar->channels, &codec_id);
if (sample_rate != st->codecpar->sample_rate) {
av_log(s1, AV_LOG_ERROR,
"sample rate %d not available, nearest is %d\n",
st->codec->sample_rate, sample_rate);
st->codecpar->sample_rate, sample_rate);
goto fail;
}

Expand Down
13 changes: 6 additions & 7 deletions libavdevice/bktr.c
Original file line number Diff line number Diff line change
Expand Up @@ -295,13 +295,12 @@ static int grab_read_header(AVFormatContext *s1)
s->height = height;
s->per_frame = ((uint64_t)1000000 * framerate.den) / framerate.num;

st->codec->codec_type = AVMEDIA_TYPE_VIDEO;
st->codec->pix_fmt = AV_PIX_FMT_YUV420P;
st->codec->codec_id = AV_CODEC_ID_RAWVIDEO;
st->codec->width = width;
st->codec->height = height;
st->codec->time_base.den = framerate.num;
st->codec->time_base.num = framerate.den;
st->codecpar->codec_type = AVMEDIA_TYPE_VIDEO;
st->codecpar->format = AV_PIX_FMT_YUV420P;
st->codecpar->codec_id = AV_CODEC_ID_RAWVIDEO;
st->codecpar->width = width;
st->codecpar->height = height;
st->avg_frame_rate = framerate;


if (bktr_init(s1->filename, width, height, s->standard,
Expand Down
16 changes: 8 additions & 8 deletions libavdevice/fbdev.c
Original file line number Diff line number Diff line change
Expand Up @@ -164,21 +164,21 @@ static av_cold int fbdev_read_header(AVFormatContext *avctx)
goto fail;
}

st->codec->codec_type = AVMEDIA_TYPE_VIDEO;
st->codec->codec_id = AV_CODEC_ID_RAWVIDEO;
st->codec->width = fbdev->width;
st->codec->height = fbdev->height;
st->codec->pix_fmt = pix_fmt;
st->codec->time_base = (AVRational){fbdev->framerate_q.den, fbdev->framerate_q.num};
st->codec->bit_rate =
st->codecpar->codec_type = AVMEDIA_TYPE_VIDEO;
st->codecpar->codec_id = AV_CODEC_ID_RAWVIDEO;
st->codecpar->width = fbdev->width;
st->codecpar->height = fbdev->height;
st->codecpar->format = pix_fmt;
st->codecpar->bit_rate =
fbdev->width * fbdev->height * fbdev->bytes_per_pixel * av_q2d(fbdev->framerate_q) * 8;
st->avg_frame_rate = fbdev->framerate_q;

av_log(avctx, AV_LOG_INFO,
"w:%d h:%d bpp:%d pixfmt:%s fps:%d/%d bit_rate:%d\n",
fbdev->width, fbdev->height, fbdev->varinfo.bits_per_pixel,
av_get_pix_fmt_name(pix_fmt),
fbdev->framerate_q.num, fbdev->framerate_q.den,
st->codec->bit_rate);
st->codecpar->bit_rate);
return 0;

fail:
Expand Down
10 changes: 5 additions & 5 deletions libavdevice/jack.c
Original file line number Diff line number Diff line change
Expand Up @@ -254,14 +254,14 @@ static int audio_read_header(AVFormatContext *context)
return AVERROR(ENOMEM);
}

stream->codec->codec_type = AVMEDIA_TYPE_AUDIO;
stream->codecpar->codec_type = AVMEDIA_TYPE_AUDIO;
#if HAVE_BIGENDIAN
stream->codec->codec_id = AV_CODEC_ID_PCM_F32BE;
stream->codecpar->codec_id = AV_CODEC_ID_PCM_F32BE;
#else
stream->codec->codec_id = AV_CODEC_ID_PCM_F32LE;
stream->codecpar->codec_id = AV_CODEC_ID_PCM_F32LE;
#endif
stream->codec->sample_rate = self->sample_rate;
stream->codec->channels = self->nports;
stream->codecpar->sample_rate = self->sample_rate;
stream->codecpar->channels = self->nports;

avpriv_set_pts_info(stream, 64, 1, 1000000); /* 64 bits pts in us */
return 0;
Expand Down
12 changes: 6 additions & 6 deletions libavdevice/libcdio.c
Original file line number Diff line number Diff line change
Expand Up @@ -85,19 +85,19 @@ static av_cold int read_header(AVFormatContext *ctx)
}
cdio_paranoia_modeset(s->paranoia, s->paranoia_mode);

st->codec->codec_type = AVMEDIA_TYPE_AUDIO;
st->codecpar->codec_type = AVMEDIA_TYPE_AUDIO;
if (s->drive->bigendianp)
st->codec->codec_id = AV_CODEC_ID_PCM_S16BE;
st->codecpar->codec_id = AV_CODEC_ID_PCM_S16BE;
else
st->codec->codec_id = AV_CODEC_ID_PCM_S16LE;
st->codec->sample_rate = 44100;
st->codec->channels = 2;
st->codecpar->codec_id = AV_CODEC_ID_PCM_S16LE;
st->codecpar->sample_rate = 44100;
st->codecpar->channels = 2;
if (s->drive->audio_last_sector != CDIO_INVALID_LSN &&
s->drive->audio_first_sector != CDIO_INVALID_LSN)
st->duration = s->drive->audio_last_sector - s->drive->audio_first_sector;
else if (s->drive->tracks)
st->duration = s->drive->disc_toc[s->drive->tracks].dwStartSector;
avpriv_set_pts_info(st, 64, CDIO_CD_FRAMESIZE_RAW, 2*st->codec->channels*st->codec->sample_rate);
avpriv_set_pts_info(st, 64, CDIO_CD_FRAMESIZE_RAW, 2 * st->codecpar->channels * st->codecpar->sample_rate);

for (i = 0; i < s->drive->tracks; i++) {
char title[16];
Expand Down
15 changes: 7 additions & 8 deletions libavdevice/libdc1394.c
Original file line number Diff line number Diff line change
Expand Up @@ -170,13 +170,12 @@ static inline int dc1394_read_common(AVFormatContext *c,
goto out;
}
avpriv_set_pts_info(vst, 64, 1, 1000);
vst->codec->codec_type = AVMEDIA_TYPE_VIDEO;
vst->codec->codec_id = AV_CODEC_ID_RAWVIDEO;
vst->codec->time_base.den = framerate.num;
vst->codec->time_base.num = framerate.den;
vst->codec->width = fmt->width;
vst->codec->height = fmt->height;
vst->codec->pix_fmt = fmt->pix_fmt;
vst->codecpar->codec_type = AVMEDIA_TYPE_VIDEO;
vst->codecpar->codec_id = AV_CODEC_ID_RAWVIDEO;
vst->codecpar->width = fmt->width;
vst->codecpar->height = fmt->height;
vst->codecpar->format = fmt->pix_fmt;
vst->avg_frame_rate = framerate;

/* packet init */
av_init_packet(&dc1394->packet);
Expand All @@ -187,7 +186,7 @@ static inline int dc1394_read_common(AVFormatContext *c,

dc1394->current_frame = 0;

vst->codec->bit_rate = av_rescale(dc1394->packet.size * 8, fps->frame_rate, 1000);
vst->codecpar->bit_rate = av_rescale(dc1394->packet.size * 8, fps->frame_rate, 1000);
*select_fps = fps;
*select_fmt = fmt;
out:
Expand Down
8 changes: 4 additions & 4 deletions libavdevice/oss_dec.c
Original file line number Diff line number Diff line change
Expand Up @@ -61,10 +61,10 @@ static int audio_read_header(AVFormatContext *s1)
}

/* take real parameters */
st->codec->codec_type = AVMEDIA_TYPE_AUDIO;
st->codec->codec_id = s->codec_id;
st->codec->sample_rate = s->sample_rate;
st->codec->channels = s->channels;
st->codecpar->codec_type = AVMEDIA_TYPE_AUDIO;
st->codecpar->codec_id = s->codec_id;
st->codecpar->sample_rate = s->sample_rate;
st->codecpar->channels = s->channels;

avpriv_set_pts_info(st, 64, 1, 1000000); /* 64 bits pts in us */
return 0;
Expand Down
4 changes: 2 additions & 2 deletions libavdevice/oss_enc.c
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,8 @@ static int audio_write_header(AVFormatContext *s1)
int ret;

st = s1->streams[0];
s->sample_rate = st->codec->sample_rate;
s->channels = st->codec->channels;
s->sample_rate = st->codecpar->sample_rate;
s->channels = st->codecpar->channels;
ret = ff_oss_audio_open(s1, 1, s1->filename);
if (ret < 0) {
return AVERROR(EIO);
Expand Down
8 changes: 4 additions & 4 deletions libavdevice/pulse.c
Original file line number Diff line number Diff line change
Expand Up @@ -107,10 +107,10 @@ static av_cold int pulse_read_header(AVFormatContext *s)
return AVERROR(EIO);
}
/* take real parameters */
st->codec->codec_type = AVMEDIA_TYPE_AUDIO;
st->codec->codec_id = codec_id;
st->codec->sample_rate = pd->sample_rate;
st->codec->channels = pd->channels;
st->codecpar->codec_type = AVMEDIA_TYPE_AUDIO;
st->codecpar->codec_id = codec_id;
st->codecpar->sample_rate = pd->sample_rate;
st->codecpar->channels = pd->channels;
avpriv_set_pts_info(st, 64, 1, 1000000); /* 64 bits pts in us */

pd->pts = AV_NOPTS_VALUE;
Expand Down
8 changes: 4 additions & 4 deletions libavdevice/sndio_dec.c
Original file line number Diff line number Diff line change
Expand Up @@ -46,10 +46,10 @@ static av_cold int audio_read_header(AVFormatContext *s1)
return ret;

/* take real parameters */
st->codec->codec_type = AVMEDIA_TYPE_AUDIO;
st->codec->codec_id = s->codec_id;
st->codec->sample_rate = s->sample_rate;
st->codec->channels = s->channels;
st->codecpar->codec_type = AVMEDIA_TYPE_AUDIO;
st->codecpar->codec_id = s->codec_id;
st->codecpar->sample_rate = s->sample_rate;
st->codecpar->channels = s->channels;

avpriv_set_pts_info(st, 64, 1, 1000000); /* 64 bits pts in us */

Expand Down
4 changes: 2 additions & 2 deletions libavdevice/sndio_enc.c
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,8 @@ static av_cold int audio_write_header(AVFormatContext *s1)
int ret;

st = s1->streams[0];
s->sample_rate = st->codec->sample_rate;
s->channels = st->codec->channels;
s->sample_rate = st->codecpar->sample_rate;
s->channels = st->codecpar->channels;

ret = ff_sndio_open(s1, 1, s1->filename);

Expand Down
18 changes: 9 additions & 9 deletions libavdevice/v4l2.c
Original file line number Diff line number Diff line change
Expand Up @@ -827,8 +827,8 @@ static int v4l2_read_header(AVFormatContext *s1)
if ((res = v4l2_set_parameters(s1) < 0))
return res;

st->codec->pix_fmt = fmt_v4l2ff(desired_format, codec_id);
s->frame_size = av_image_get_buffer_size(st->codec->pix_fmt,
st->codecpar->format = fmt_v4l2ff(desired_format, codec_id);
s->frame_size = av_image_get_buffer_size(st->codecpar->format,
s->width, s->height, 1);

if ((res = mmap_init(s1)) ||
Expand All @@ -839,14 +839,14 @@ static int v4l2_read_header(AVFormatContext *s1)

s->top_field_first = first_field(s->fd);

st->codec->codec_type = AVMEDIA_TYPE_VIDEO;
st->codec->codec_id = codec_id;
st->codecpar->codec_type = AVMEDIA_TYPE_VIDEO;
st->codecpar->codec_id = codec_id;
if (codec_id == AV_CODEC_ID_RAWVIDEO)
st->codec->codec_tag =
avcodec_pix_fmt_to_codec_tag(st->codec->pix_fmt);
st->codec->width = s->width;
st->codec->height = s->height;
st->codec->bit_rate = s->frame_size * av_q2d(st->avg_frame_rate) * 8;
st->codecpar->codec_tag =
avcodec_pix_fmt_to_codec_tag(st->codecpar->format);
st->codecpar->width = s->width;
st->codecpar->height = s->height;
st->codecpar->bit_rate = s->frame_size * av_q2d(st->avg_frame_rate) * 8;

return 0;
}
Expand Down
35 changes: 18 additions & 17 deletions libavdevice/vfwcap.c
Original file line number Diff line number Diff line change
Expand Up @@ -244,7 +244,7 @@ static int vfw_read_close(AVFormatContext *s)
static int vfw_read_header(AVFormatContext *s)
{
struct vfw_ctx *ctx = s->priv_data;
AVCodecContext *codec;
AVCodecParameters *par;
AVStream *st;
int devnum;
int bisize;
Expand Down Expand Up @@ -373,29 +373,30 @@ static int vfw_read_header(AVFormatContext *s)
if(!ret)
goto fail_io;

codec = st->codec;
codec->time_base = (AVRational){framerate_q.den, framerate_q.num};
codec->codec_type = AVMEDIA_TYPE_VIDEO;
codec->width = bi->bmiHeader.biWidth;
codec->height = bi->bmiHeader.biHeight;
codec->pix_fmt = vfw_pixfmt(biCompression, biBitCount);
if(codec->pix_fmt == AV_PIX_FMT_NONE) {
codec->codec_id = vfw_codecid(biCompression);
if(codec->codec_id == AV_CODEC_ID_NONE) {
st->avg_frame_rate = framerate_q;

par = st->codecpar;
par->codec_type = AVMEDIA_TYPE_VIDEO;
par->width = bi->bmiHeader.biWidth;
par->height = bi->bmiHeader.biHeight;
par->format = vfw_pixfmt(biCompression, biBitCount);
if (par->format == AV_PIX_FMT_NONE) {
par->codec_id = vfw_codecid(biCompression);
if (par->codec_id == AV_CODEC_ID_NONE) {
av_log(s, AV_LOG_ERROR, "Unknown compression type. "
"Please report verbose (-v 9) debug information.\n");
vfw_read_close(s);
return AVERROR_PATCHWELCOME;
}
codec->bits_per_coded_sample = biBitCount;
par->bits_per_coded_sample = biBitCount;
} else {
codec->codec_id = AV_CODEC_ID_RAWVIDEO;
par->codec_id = AV_CODEC_ID_RAWVIDEO;
if(biCompression == BI_RGB) {
codec->bits_per_coded_sample = biBitCount;
codec->extradata = av_malloc(9 + AV_INPUT_BUFFER_PADDING_SIZE);
if (codec->extradata) {
codec->extradata_size = 9;
memcpy(codec->extradata, "BottomUp", 9);
par->bits_per_coded_sample = biBitCount;
par->extradata = av_malloc(9 + AV_INPUT_BUFFER_PADDING_SIZE);
if (par->extradata) {
par->extradata_size = 9;
memcpy(par->extradata, "BottomUp", 9);
}
}
}
Expand Down
15 changes: 8 additions & 7 deletions libavdevice/x11grab.c
Original file line number Diff line number Diff line change
Expand Up @@ -347,16 +347,17 @@ static int x11grab_read_header(AVFormatContext *s1)
x11grab->image = image;
x11grab->use_shm = use_shm;

ret = pixfmt_from_image(s1, image, &st->codec->pix_fmt);
ret = pixfmt_from_image(s1, image, &st->codecpar->format);
if (ret < 0)
goto out;

st->codec->codec_type = AVMEDIA_TYPE_VIDEO;
st->codec->codec_id = AV_CODEC_ID_RAWVIDEO;
st->codec->width = x11grab->width;
st->codec->height = x11grab->height;
st->codec->time_base = x11grab->time_base;
st->codec->bit_rate = x11grab->frame_size * 1 / av_q2d(x11grab->time_base) * 8;
st->codecpar->codec_type = AVMEDIA_TYPE_VIDEO;
st->codecpar->codec_id = AV_CODEC_ID_RAWVIDEO;
st->codecpar->width = x11grab->width;
st->codecpar->height = x11grab->height;
st->codecpar->bit_rate = x11grab->frame_size * 1 / av_q2d(x11grab->time_base) * 8;

st->avg_frame_rate = av_inv_q(x11grab->time_base);

out:
av_free(param);
Expand Down
11 changes: 5 additions & 6 deletions libavdevice/xcbgrab.c
Original file line number Diff line number Diff line change
Expand Up @@ -544,13 +544,12 @@ static int create_stream(AVFormatContext *s)
st->avg_frame_rate.num };
c->time_frame = av_gettime();

st->codec->codec_type = AVMEDIA_TYPE_VIDEO;
st->codec->codec_id = AV_CODEC_ID_RAWVIDEO;
st->codec->width = c->width;
st->codec->height = c->height;
st->codec->time_base = c->time_base;
st->codecpar->codec_type = AVMEDIA_TYPE_VIDEO;
st->codecpar->codec_id = AV_CODEC_ID_RAWVIDEO;
st->codecpar->width = c->width;
st->codecpar->height = c->height;

ret = pixfmt_from_pixmap_format(s, geo->depth, &st->codec->pix_fmt);
ret = pixfmt_from_pixmap_format(s, geo->depth, &st->codecpar->format);

free(geo);

Expand Down
Loading

0 comments on commit 9200514

Please sign in to comment.