Browse code

Merge commit '998e1b8f521b73e1ed3a13caaabcf79eb401cf0d'

* commit '998e1b8f521b73e1ed3a13caaabcf79eb401cf0d':
lavc: add codec parameters API

Fixes added in:
- bit_rate has been made int64_t to match.
- profile and level are properly initialize.

Merged-by: Derek Buitenhuis <derek.buitenhuis@gmail.com>

Derek Buitenhuis authored on 2016/04/01 05:18:53
Showing 4 changed files
... ...
@@ -15,6 +15,9 @@ libavutil:     2015-08-28
15 15
 
16 16
 API changes, most recent first:
17 17
 
18
+2016-xx-xx - lavc 57.33.0 - avcodec.h
19
+  xxxxxxx - Add AVCodecParameters and its related API.
20
+
18 21
 2016-03-11 - xxxxxxx - lavf/lavc 57.28.101
19 22
   Add requirement to bitstream filtering API that returned packets with
20 23
   size == 0 and side_data_elems == 0 are to be skipped by the caller.
... ...
@@ -3786,6 +3786,127 @@ typedef struct AVSubtitle {
3786 3786
 } AVSubtitle;
3787 3787
 
3788 3788
 /**
3789
+ * This struct describes the properties of an encoded stream.
3790
+ *
3791
+ * sizeof(AVCodecParameters) is not a part of the public ABI, this struct must
3792
+ * be allocated with avcodec_parameters_alloc() and freed with
3793
+ * avcodec_parameters_free().
3794
+ */
3795
+typedef struct AVCodecParameters {
3796
+    /**
3797
+     * General type of the encoded data.
3798
+     */
3799
+    enum AVMediaType codec_type;
3800
+    /**
3801
+     * Specific type of the encoded data (the codec used).
3802
+     */
3803
+    enum AVCodecID   codec_id;
3804
+    /**
3805
+     * Additional information about the codec (corresponds to the AVI FOURCC).
3806
+     */
3807
+    uint32_t         codec_tag;
3808
+
3809
+    /**
3810
+     * Extra binary data needed for initializing the decoder, codec-dependent.
3811
+     *
3812
+     * Must be allocated with av_malloc() and will be freed by
3813
+     * avcodec_parameters_free(). The allocated size of extradata must be at
3814
+     * least extradata_size + AV_INPUT_BUFFER_PADDING_SIZE, with the padding
3815
+     * bytes zeroed.
3816
+     */
3817
+    uint8_t *extradata;
3818
+    /**
3819
+     * Size of the extradata content in bytes.
3820
+     */
3821
+    int      extradata_size;
3822
+
3823
+    /**
3824
+     * - video: the pixel format, the value corresponds to enum AVPixelFormat.
3825
+     * - audio: the sample format, the value corresponds to enum AVSampleFormat.
3826
+     */
3827
+    int format;
3828
+
3829
+    /**
3830
+     * The average bitrate of the encoded data (in bits per second).
3831
+     */
3832
+    int64_t bit_rate;
3833
+
3834
+    int bits_per_coded_sample;
3835
+
3836
+    /**
3837
+     * Codec-specific bitstream restrictions that the stream conforms to.
3838
+     */
3839
+    int profile;
3840
+    int level;
3841
+
3842
+    /**
3843
+     * Video only. The dimensions of the video frame in pixels.
3844
+     */
3845
+    int width;
3846
+    int height;
3847
+
3848
+    /**
3849
+     * Video only. The aspect ratio (width / height) which a single pixel
3850
+     * should have when displayed.
3851
+     *
3852
+     * When the aspect ratio is unknown / undefined, the numerator should be
3853
+     * set to 0 (the denominator may have any value).
3854
+     */
3855
+    AVRational sample_aspect_ratio;
3856
+
3857
+    /**
3858
+     * Video only. The order of the fields in interlaced video.
3859
+     */
3860
+    enum AVFieldOrder                  field_order;
3861
+
3862
+    /**
3863
+     * Video only. Additional colorspace characteristics.
3864
+     */
3865
+    enum AVColorRange                  color_range;
3866
+    enum AVColorPrimaries              color_primaries;
3867
+    enum AVColorTransferCharacteristic color_trc;
3868
+    enum AVColorSpace                  color_space;
3869
+    enum AVChromaLocation              chroma_location;
3870
+
3871
+    /**
3872
+     * Audio only. The channel layout bitmask. May be 0 if the channel layout is
3873
+     * unknown or unspecified, otherwise the number of bits set must be equal to
3874
+     * the channels field.
3875
+     */
3876
+    uint64_t channel_layout;
3877
+    /**
3878
+     * Audio only. The number of audio channels.
3879
+     */
3880
+    int      channels;
3881
+    /**
3882
+     * Audio only. The number of audio samples per second.
3883
+     */
3884
+    int      sample_rate;
3885
+    /**
3886
+     * Audio only. The number of bytes per coded audio frame, required by some
3887
+     * formats.
3888
+     *
3889
+     * Corresponds to nBlockAlign in WAVEFORMATEX.
3890
+     */
3891
+    int      block_align;
3892
+
3893
+    /**
3894
+     * Audio only. The amount of padding (in samples) inserted by the encoder at
3895
+     * the beginning of the audio. I.e. this number of leading decoded samples
3896
+     * must be discarded by the caller to get the original audio without leading
3897
+     * padding.
3898
+     */
3899
+    int initial_padding;
3900
+    /**
3901
+     * Audio only. The amount of padding (in samples) appended by the encoder to
3902
+     * the end of the audio. I.e. this number of decoded samples must be
3903
+     * discarded by the caller from the end of the stream to get the original
3904
+     * audio without any trailing padding.
3905
+     */
3906
+    int trailing_padding;
3907
+} AVCodecParameters;
3908
+
3909
+/**
3789 3910
  * If c is NULL, returns the first registered codec,
3790 3911
  * if c is non-NULL, returns the next registered codec after c,
3791 3912
  * or NULL if c is the last one.
... ...
@@ -3900,6 +4021,48 @@ const AVClass *avcodec_get_subtitle_rect_class(void);
3900 3900
 int avcodec_copy_context(AVCodecContext *dest, const AVCodecContext *src);
3901 3901
 
3902 3902
 /**
3903
+ * Allocate a new AVCodecParameters and set its fields to default values
3904
+ * (unknown/invalid/0). The returned struct must be freed with
3905
+ * avcodec_parameters_free().
3906
+ */
3907
+AVCodecParameters *avcodec_parameters_alloc(void);
3908
+
3909
+/**
3910
+ * Free an AVCodecParameters instance and everything associated with it and
3911
+ * write NULL to the supplied pointer.
3912
+ */
3913
+void avcodec_parameters_free(AVCodecParameters **par);
3914
+
3915
+/**
3916
+ * Copy the contents of src to dst. Any allocated fields in dst are freed and
3917
+ * replaced with newly allocated duplicates of the corresponding fields in src.
3918
+ *
3919
+ * @return >= 0 on success, a negative AVERROR code on failure.
3920
+ */
3921
+int avcodec_parameters_copy(AVCodecParameters *dst, const AVCodecParameters *src);
3922
+
3923
+/**
3924
+ * Fill the parameters struct based on the values from the supplied codec
3925
+ * context. Any allocated fields in par are freed and replaced with duplicates
3926
+ * of the corresponding fields in codec.
3927
+ *
3928
+ * @return >= 0 on success, a negative AVERROR code on failure
3929
+ */
3930
+int avcodec_parameters_from_context(AVCodecParameters *par,
3931
+                                    const AVCodecContext *codec);
3932
+
3933
+/**
3934
+ * Fill the codec context based on the values from the supplied codec
3935
+ * parameters. Any allocated fields in codec that have a corresponding field in
3936
+ * par are freed and replaced with duplicates of the corresponding field in par.
3937
+ * Fields in codec that do not have a counterpart in par are not touched.
3938
+ *
3939
+ * @return >= 0 on success, a negative AVERROR code on failure.
3940
+ */
3941
+int avcodec_parameters_to_context(AVCodecContext *codec,
3942
+                                  const AVCodecParameters *par);
3943
+
3944
+/**
3903 3945
  * Initialize the AVCodecContext to use the given AVCodec. Prior to using this
3904 3946
  * function the context has to be allocated with avcodec_alloc_context3().
3905 3947
  *
... ...
@@ -3680,6 +3680,158 @@ AVCPBProperties *ff_add_cpb_side_data(AVCodecContext *avctx)
3680 3680
     return props;
3681 3681
 }
3682 3682
 
3683
+static void codec_parameters_reset(AVCodecParameters *par)
3684
+{
3685
+    av_freep(&par->extradata);
3686
+
3687
+    memset(par, 0, sizeof(*par));
3688
+
3689
+    par->codec_type          = AVMEDIA_TYPE_UNKNOWN;
3690
+    par->codec_id            = AV_CODEC_ID_NONE;
3691
+    par->format              = -1;
3692
+    par->field_order         = AV_FIELD_UNKNOWN;
3693
+    par->color_range         = AVCOL_RANGE_UNSPECIFIED;
3694
+    par->color_primaries     = AVCOL_PRI_UNSPECIFIED;
3695
+    par->color_trc           = AVCOL_TRC_UNSPECIFIED;
3696
+    par->color_space         = AVCOL_SPC_UNSPECIFIED;
3697
+    par->chroma_location     = AVCHROMA_LOC_UNSPECIFIED;
3698
+    par->sample_aspect_ratio = (AVRational){ 0, 1 };
3699
+}
3700
+
3701
+AVCodecParameters *avcodec_parameters_alloc(void)
3702
+{
3703
+    AVCodecParameters *par = av_mallocz(sizeof(*par));
3704
+
3705
+    if (!par)
3706
+        return NULL;
3707
+    codec_parameters_reset(par);
3708
+    return par;
3709
+}
3710
+
3711
+void avcodec_parameters_free(AVCodecParameters **ppar)
3712
+{
3713
+    AVCodecParameters *par = *ppar;
3714
+
3715
+    if (!par)
3716
+        return;
3717
+    codec_parameters_reset(par);
3718
+
3719
+    av_freep(ppar);
3720
+}
3721
+
3722
+int avcodec_parameters_copy(AVCodecParameters *dst, const AVCodecParameters *src)
3723
+{
3724
+    codec_parameters_reset(dst);
3725
+    memcpy(dst, src, sizeof(*dst));
3726
+
3727
+    dst->extradata      = NULL;
3728
+    dst->extradata_size = 0;
3729
+    if (src->extradata) {
3730
+        dst->extradata = av_mallocz(src->extradata_size + AV_INPUT_BUFFER_PADDING_SIZE);
3731
+        if (!dst->extradata)
3732
+            return AVERROR(ENOMEM);
3733
+        memcpy(dst->extradata, src->extradata, src->extradata_size);
3734
+        dst->extradata_size = src->extradata_size;
3735
+    }
3736
+
3737
+    return 0;
3738
+}
3739
+
3740
+int avcodec_parameters_from_context(AVCodecParameters *par,
3741
+                                    const AVCodecContext *codec)
3742
+{
3743
+    codec_parameters_reset(par);
3744
+
3745
+    par->codec_type = codec->codec_type;
3746
+    par->codec_id   = codec->codec_id;
3747
+    par->codec_tag  = codec->codec_tag;
3748
+
3749
+    par->bit_rate              = codec->bit_rate;
3750
+    par->bits_per_coded_sample = codec->bits_per_coded_sample;
3751
+    par->profile               = codec->profile;
3752
+    par->level                 = codec->level;
3753
+
3754
+    switch (par->codec_type) {
3755
+    case AVMEDIA_TYPE_VIDEO:
3756
+        par->format              = codec->pix_fmt;
3757
+        par->width               = codec->width;
3758
+        par->height              = codec->height;
3759
+        par->field_order         = codec->field_order;
3760
+        par->color_range         = codec->color_range;
3761
+        par->color_primaries     = codec->color_primaries;
3762
+        par->color_trc           = codec->color_trc;
3763
+        par->color_space         = codec->colorspace;
3764
+        par->chroma_location     = codec->chroma_sample_location;
3765
+        par->sample_aspect_ratio = codec->sample_aspect_ratio;
3766
+        break;
3767
+    case AVMEDIA_TYPE_AUDIO:
3768
+        par->format          = codec->sample_fmt;
3769
+        par->channel_layout  = codec->channel_layout;
3770
+        par->channels        = codec->channels;
3771
+        par->sample_rate     = codec->sample_rate;
3772
+        par->block_align     = codec->block_align;
3773
+        par->initial_padding = codec->initial_padding;
3774
+        break;
3775
+    }
3776
+
3777
+    if (codec->extradata) {
3778
+        par->extradata = av_mallocz(codec->extradata_size + AV_INPUT_BUFFER_PADDING_SIZE);
3779
+        if (!par->extradata)
3780
+            return AVERROR(ENOMEM);
3781
+        memcpy(par->extradata, codec->extradata, codec->extradata_size);
3782
+        par->extradata_size = codec->extradata_size;
3783
+    }
3784
+
3785
+    return 0;
3786
+}
3787
+
3788
+int avcodec_parameters_to_context(AVCodecContext *codec,
3789
+                                  const AVCodecParameters *par)
3790
+{
3791
+    codec->codec_type = par->codec_type;
3792
+    codec->codec_id   = par->codec_id;
3793
+    codec->codec_tag  = par->codec_tag;
3794
+
3795
+    codec->bit_rate              = par->bit_rate;
3796
+    codec->bits_per_coded_sample = par->bits_per_coded_sample;
3797
+    codec->profile               = par->profile;
3798
+    codec->level                 = par->level;
3799
+
3800
+    switch (par->codec_type) {
3801
+    case AVMEDIA_TYPE_VIDEO:
3802
+        codec->pix_fmt                = par->format;
3803
+        codec->width                  = par->width;
3804
+        codec->height                 = par->height;
3805
+        codec->field_order            = par->field_order;
3806
+        codec->color_range            = par->color_range;
3807
+        codec->color_primaries        = par->color_primaries;
3808
+        codec->color_trc              = par->color_trc;
3809
+        codec->colorspace             = par->color_space;
3810
+        codec->chroma_sample_location = par->chroma_location;
3811
+        codec->sample_aspect_ratio    = par->sample_aspect_ratio;
3812
+        break;
3813
+    case AVMEDIA_TYPE_AUDIO:
3814
+        codec->sample_fmt      = par->format;
3815
+        codec->channel_layout  = par->channel_layout;
3816
+        codec->channels        = par->channels;
3817
+        codec->sample_rate     = par->sample_rate;
3818
+        codec->block_align     = par->block_align;
3819
+        codec->initial_padding = par->initial_padding;
3820
+        break;
3821
+    }
3822
+
3823
+    if (par->extradata) {
3824
+        av_freep(&codec->extradata);
3825
+        codec->extradata = av_mallocz(par->extradata_size + AV_INPUT_BUFFER_PADDING_SIZE);
3826
+        if (!codec->extradata)
3827
+            return AVERROR(ENOMEM);
3828
+        memcpy(codec->extradata, par->extradata, par->extradata_size);
3829
+        codec->extradata_size = par->extradata_size;
3830
+    }
3831
+
3832
+    return 0;
3833
+}
3834
+
3683 3835
 #ifdef TEST
3684 3836
 int main(void){
3685 3837
     AVCodec *codec = NULL;
... ...
@@ -28,7 +28,7 @@
28 28
 #include "libavutil/version.h"
29 29
 
30 30
 #define LIBAVCODEC_VERSION_MAJOR  57
31
-#define LIBAVCODEC_VERSION_MINOR  32
31
+#define LIBAVCODEC_VERSION_MINOR  33
32 32
 #define LIBAVCODEC_VERSION_MICRO 100
33 33
 
34 34
 #define LIBAVCODEC_VERSION_INT  AV_VERSION_INT(LIBAVCODEC_VERSION_MAJOR, \