History

lavc: add codec parameters API

This API is intended to allow passing around codec parameters without using full AVCodecContext (which also contains codec options and encoder/decoder state).

Anton Khirnov committed 12 years ago

Commit 998e1b81 parent 257b30a4 files · +319 −1Browse files

Unified · Split

4 files
  1. doc/APIchanges +3 −0
  2. libavcodec/avcodec.h +163 −0
  3. libavcodec/utils.c +152 −0
  4. libavcodec/version.h +1 −1
doc/APIchanges+3−0
@@ -13,6 +13,9 @@ libavutil: 2015-08-28 · 12 unmodified lines
1313
1414 API changes, most recent first:
1515
16+2016-xx-xx - lavc 57.14.0 - avcodec.h
17+ xxxxxxx - Add AVCodecParameters and its related API.
18+
1619 2016-xx-xx - xxxxxxx - lavf 57.4.0 - avformat.h
1720 Add AVFormatContext.protocol_whitelist and protocol_blacklist.
1821 Add 'protocol_whitelist' and 'protocol_blacklist' private options for
libavcodec/avcodec.h+163−0
@@ -3335,6 +3335,127 @@ typedef struct AVSubtitle { · 3334 unmodified lines
33353335 int64_t pts; ///< Same as packet pts, in AV_TIME_BASE
33363336 } AVSubtitle;
33373337
3338+/**
3339+ * This struct describes the properties of an encoded stream.
3340+ *
3341+ * sizeof(AVCodecParameters) is not a part of the public ABI, this struct must
3342+ * be allocated with avcodec_parameters_alloc() and freed with
3343+ * avcodec_parameters_free().
3344+ */
3345+typedef struct AVCodecParameters {
3346+ /**
3347+ * General type of the encoded data.
3348+ */
3349+ enum AVMediaType codec_type;
3350+ /**
3351+ * Specific type of the encoded data (the codec used).
3352+ */
3353+ enum AVCodecID codec_id;
3354+ /**
3355+ * Additional information about the codec (corresponds to the AVI FOURCC).
3356+ */
3357+ uint32_t codec_tag;
3358+
3359+ /**
3360+ * Extra binary data needed for initializing the decoder, codec-dependent.
3361+ *
3362+ * Must be allocated with av_malloc() and will be freed by
3363+ * avcodec_parameters_free(). The allocated size of extradata must be at
3364+ * least extradata_size + AV_INPUT_BUFFER_PADDING_SIZE, with the padding
3365+ * bytes zeroed.
3366+ */
3367+ uint8_t *extradata;
3368+ /**
3369+ * Size of the extradata content in bytes.
3370+ */
3371+ int extradata_size;
3372+
3373+ /**
3374+ * - video: the pixel format, the value corresponds to enum AVPixelFormat.
3375+ * - audio: the sample format, the value corresponds to enum AVSampleFormat.
3376+ */
3377+ int format;
3378+
3379+ /**
3380+ * The average bitrate of the encoded data (in bits per second).
3381+ */
3382+ int bit_rate;
3383+
3384+ int bits_per_coded_sample;
3385+
3386+ /**
3387+ * Codec-specific bitstream restrictions that the stream conforms to.
3388+ */
3389+ int profile;
3390+ int level;
3391+
3392+ /**
3393+ * Video only. The dimensions of the video frame in pixels.
3394+ */
3395+ int width;
3396+ int height;
3397+
3398+ /**
3399+ * Video only. The aspect ratio (width / height) which a single pixel
3400+ * should have when displayed.
3401+ *
3402+ * When the aspect ratio is unknown / undefined, the numerator should be
3403+ * set to 0 (the denominator may have any value).
3404+ */
3405+ AVRational sample_aspect_ratio;
3406+
3407+ /**
3408+ * Video only. The order of the fields in interlaced video.
3409+ */
3410+ enum AVFieldOrder field_order;
3411+
3412+ /**
3413+ * Video only. Additional colorspace characteristics.
3414+ */
3415+ enum AVColorRange color_range;
3416+ enum AVColorPrimaries color_primaries;
3417+ enum AVColorTransferCharacteristic color_trc;
3418+ enum AVColorSpace color_space;
3419+ enum AVChromaLocation chroma_location;
3420+
3421+ /**
3422+ * Audio only. The channel layout bitmask. May be 0 if the channel layout is
3423+ * unknown or unspecified, otherwise the number of bits set must be equal to
3424+ * the channels field.
3425+ */
3426+ uint64_t channel_layout;
3427+ /**
3428+ * Audio only. The number of audio channels.
3429+ */
3430+ int channels;
3431+ /**
3432+ * Audio only. The number of audio samples per second.
3433+ */
3434+ int sample_rate;
3435+ /**
3436+ * Audio only. The number of bytes per coded audio frame, required by some
3437+ * formats.
3438+ *
3439+ * Corresponds to nBlockAlign in WAVEFORMATEX.
3440+ */
3441+ int block_align;
3442+
3443+ /**
3444+ * Audio only. The amount of padding (in samples) inserted by the encoder at
3445+ * the beginning of the audio. I.e. this number of leading decoded samples
3446+ * must be discarded by the caller to get the original audio without leading
3447+ * padding.
3448+ */
3449+ int initial_padding;
3450+ /**
3451+ * Audio only. The amount of padding (in samples) appended by the encoder to
3452+ * the end of the audio. I.e. this number of decoded samples must be
3453+ * discarded by the caller from the end of the stream to get the original
3454+ * audio without any trailing padding.
3455+ */
3456+ int trailing_padding;
3457+} AVCodecParameters;
3458+
33383459 /**
33393460 * If c is NULL, returns the first registered codec,
33403461 * if c is non-NULL, returns the next registered codec after c,
@@ -3433,6 +3554,48 @@ const AVClass *avcodec_get_class(void); · 92 unmodified lines
34333554 */
34343555 int avcodec_copy_context(AVCodecContext *dest, const AVCodecContext *src);
34353556
3557+/**
3558+ * Allocate a new AVCodecParameters and set its fields to default values
3559+ * (unknown/invalid/0). The returned struct must be freed with
3560+ * avcodec_parameters_free().
3561+ */
3562+AVCodecParameters *avcodec_parameters_alloc(void);
3563+
3564+/**
3565+ * Free an AVCodecParameters instance and everything associated with it and
3566+ * write NULL to the supplied pointer.
3567+ */
3568+void avcodec_parameters_free(AVCodecParameters **par);
3569+
3570+/**
3571+ * Copy the contents of src to dst. Any allocated fields in dst are freed and
3572+ * replaced with newly allocated duplicates of the corresponding fields in src.
3573+ *
3574+ * @return >= 0 on success, a negative AVERROR code on failure.
3575+ */
3576+int avcodec_parameters_copy(AVCodecParameters *dst, const AVCodecParameters *src);
3577+
3578+/**
3579+ * Fill the parameters struct based on the values from the supplied codec
3580+ * context. Any allocated fields in par are freed and replaced with duplicates
3581+ * of the corresponding fields in codec.
3582+ *
3583+ * @return >= 0 on success, a negative AVERROR code on failure
3584+ */
3585+int avcodec_parameters_from_context(AVCodecParameters *par,
3586+ const AVCodecContext *codec);
3587+
3588+/**
3589+ * Fill the codec context based on the values from the supplied codec
3590+ * parameters. Any allocated fields in codec that have a corresponding field in
3591+ * par are freed and replaced with duplicates of the corresponding field in par.
3592+ * Fields in codec that do not have a counterpart in par are not touched.
3593+ *
3594+ * @return >= 0 on success, a negative AVERROR code on failure.
3595+ */
3596+int avcodec_parameters_to_context(AVCodecContext *codec,
3597+ const AVCodecParameters *par);
3598+
34363599 /**
34373600 * Initialize the AVCodecContext to use the given AVCodec. Prior to using this
34383601 * function the context has to be allocated with avcodec_alloc_context3().
libavcodec/utils.c+152−0
@@ -2440,3 +2440,155 @@ AVCPBProperties *ff_add_cpb_side_data(AVCodecContext *avctx) · 2439 unmodified lines
24402440
24412441 return props;
24422442 }
2443+
2444+static void codec_parameters_reset(AVCodecParameters *par)
2445+{
2446+ av_freep(&par->extradata);
2447+
2448+ memset(par, 0, sizeof(*par));
2449+
2450+ par->codec_type = AVMEDIA_TYPE_UNKNOWN;
2451+ par->codec_id = AV_CODEC_ID_NONE;
2452+ par->format = -1;
2453+ par->field_order = AV_FIELD_UNKNOWN;
2454+ par->color_range = AVCOL_RANGE_UNSPECIFIED;
2455+ par->color_primaries = AVCOL_PRI_UNSPECIFIED;
2456+ par->color_trc = AVCOL_TRC_UNSPECIFIED;
2457+ par->color_space = AVCOL_SPC_UNSPECIFIED;
2458+ par->chroma_location = AVCHROMA_LOC_UNSPECIFIED;
2459+ par->sample_aspect_ratio = (AVRational){ 0, 1 };
2460+}
2461+
2462+AVCodecParameters *avcodec_parameters_alloc(void)
2463+{
2464+ AVCodecParameters *par = av_mallocz(sizeof(*par));
2465+
2466+ if (!par)
2467+ return NULL;
2468+ codec_parameters_reset(par);
2469+ return par;
2470+}
2471+
2472+void avcodec_parameters_free(AVCodecParameters **ppar)
2473+{
2474+ AVCodecParameters *par = *ppar;
2475+
2476+ if (!par)
2477+ return;
2478+ codec_parameters_reset(par);
2479+
2480+ av_freep(ppar);
2481+}
2482+
2483+int avcodec_parameters_copy(AVCodecParameters *dst, const AVCodecParameters *src)
2484+{
2485+ codec_parameters_reset(dst);
2486+ memcpy(dst, src, sizeof(*dst));
2487+
2488+ dst->extradata = NULL;
2489+ dst->extradata_size = 0;
2490+ if (src->extradata) {
2491+ dst->extradata = av_mallocz(src->extradata_size + AV_INPUT_BUFFER_PADDING_SIZE);
2492+ if (!dst->extradata)
2493+ return AVERROR(ENOMEM);
2494+ memcpy(dst->extradata, src->extradata, src->extradata_size);
2495+ dst->extradata_size = src->extradata_size;
2496+ }
2497+
2498+ return 0;
2499+}
2500+
2501+int avcodec_parameters_from_context(AVCodecParameters *par,
2502+ const AVCodecContext *codec)
2503+{
2504+ codec_parameters_reset(par);
2505+
2506+ par->codec_type = codec->codec_type;
2507+ par->codec_id = codec->codec_id;
2508+ par->codec_tag = codec->codec_tag;
2509+
2510+ par->bit_rate = codec->bit_rate;
2511+ par->bits_per_coded_sample = codec->bits_per_coded_sample;
2512+ par->profile = codec->profile;
2513+ par->level = codec->level;
2514+
2515+ switch (par->codec_type) {
2516+ case AVMEDIA_TYPE_VIDEO:
2517+ par->format = codec->pix_fmt;
2518+ par->width = codec->width;
2519+ par->height = codec->height;
2520+ par->field_order = codec->field_order;
2521+ par->color_range = codec->color_range;
2522+ par->color_primaries = codec->color_primaries;
2523+ par->color_trc = codec->color_trc;
2524+ par->color_space = codec->colorspace;
2525+ par->chroma_location = codec->chroma_sample_location;
2526+ par->sample_aspect_ratio = codec->sample_aspect_ratio;
2527+ break;
2528+ case AVMEDIA_TYPE_AUDIO:
2529+ par->format = codec->sample_fmt;
2530+ par->channel_layout = codec->channel_layout;
2531+ par->channels = codec->channels;
2532+ par->sample_rate = codec->sample_rate;
2533+ par->block_align = codec->block_align;
2534+ par->initial_padding = codec->initial_padding;
2535+ break;
2536+ }
2537+
2538+ if (codec->extradata) {
2539+ par->extradata = av_mallocz(codec->extradata_size + AV_INPUT_BUFFER_PADDING_SIZE);
2540+ if (!par->extradata)
2541+ return AVERROR(ENOMEM);
2542+ memcpy(par->extradata, codec->extradata, codec->extradata_size);
2543+ par->extradata_size = codec->extradata_size;
2544+ }
2545+
2546+ return 0;
2547+}
2548+
2549+int avcodec_parameters_to_context(AVCodecContext *codec,
2550+ const AVCodecParameters *par)
2551+{
2552+ codec->codec_type = par->codec_type;
2553+ codec->codec_id = par->codec_id;
2554+ codec->codec_tag = par->codec_tag;
2555+
2556+ codec->bit_rate = par->bit_rate;
2557+ codec->bits_per_coded_sample = par->bits_per_coded_sample;
2558+ codec->profile = par->profile;
2559+ codec->level = par->level;
2560+
2561+ switch (par->codec_type) {
2562+ case AVMEDIA_TYPE_VIDEO:
2563+ codec->pix_fmt = par->format;
2564+ codec->width = par->width;
2565+ codec->height = par->height;
2566+ codec->field_order = par->field_order;
2567+ codec->color_range = par->color_range;
2568+ codec->color_primaries = par->color_primaries;
2569+ codec->color_trc = par->color_trc;
2570+ codec->colorspace = par->color_space;
2571+ codec->chroma_sample_location = par->chroma_location;
2572+ codec->sample_aspect_ratio = par->sample_aspect_ratio;
2573+ break;
2574+ case AVMEDIA_TYPE_AUDIO:
2575+ codec->sample_fmt = par->format;
2576+ codec->channel_layout = par->channel_layout;
2577+ codec->channels = par->channels;
2578+ codec->sample_rate = par->sample_rate;
2579+ codec->block_align = par->block_align;
2580+ codec->initial_padding = par->initial_padding;
2581+ break;
2582+ }
2583+
2584+ if (par->extradata) {
2585+ av_freep(&codec->extradata);
2586+ codec->extradata = av_mallocz(par->extradata_size + AV_INPUT_BUFFER_PADDING_SIZE);
2587+ if (!codec->extradata)
2588+ return AVERROR(ENOMEM);
2589+ memcpy(codec->extradata, par->extradata, par->extradata_size);
2590+ codec->extradata_size = par->extradata_size;
2591+ }
2592+
2593+ return 0;
2594+}
libavcodec/version.h+1−1
@@ -28,7 +28,7 @@ · 27 unmodified lines
2828 #include "libavutil/version.h"
2929
3030 #define LIBAVCODEC_VERSION_MAJOR 57
31−#define LIBAVCODEC_VERSION_MINOR 13
31+#define LIBAVCODEC_VERSION_MINOR 14
3232 #define LIBAVCODEC_VERSION_MICRO 0
3333
3434 #define LIBAVCODEC_VERSION_INT AV_VERSION_INT(LIBAVCODEC_VERSION_MAJOR, \

Keyboard shortcuts

General

Show keyboard shortcuts
?
Focus the current search
/

Issues and pull requests

Open filters
f

Issues, pull requests and Inbox

Select next item
j↓
Select previous item
k↑
Open selected item
Enter
Dismiss filters or clear selection
Esc

Repository code

Go to file
t