diff options
author | Clément Bœsch <ubitux@gmail.com> | 2012-12-29 22:09:59 +0100 |
---|---|---|
committer | Clément Bœsch <ubitux@gmail.com> | 2012-12-30 22:19:04 +0100 |
commit | 36e61e24e7ac737b38c4382d439329352d9e0c29 (patch) | |
tree | a3a478a143dcbb3d4d386fabfa1513487543c438 /libavcodec | |
parent | e911f4ae720afe4a090ded2b165647734f0320ef (diff) | |
download | ffmpeg-36e61e24e7ac737b38c4382d439329352d9e0c29.tar.gz |
lavc: add ff_bprint_to_extradata() helper and use it.
This commit also makes sure the extradata and subtitle_header are NUL
terminated, without taking into account the trailing '\0' in account in
the size.
At the same time, it should fix 'warning: dereferencing type-punned
pointer will break strict-aliasing rules' warning for compilers who
don't consider uint8_t** and char** compatibles.
Diffstat (limited to 'libavcodec')
-rw-r--r-- | libavcodec/assdec.c | 3 | ||||
-rw-r--r-- | libavcodec/assenc.c | 3 | ||||
-rw-r--r-- | libavcodec/dvdsubenc.c | 5 | ||||
-rw-r--r-- | libavcodec/internal.h | 5 | ||||
-rw-r--r-- | libavcodec/utils.c | 19 |
5 files changed, 31 insertions, 4 deletions
diff --git a/libavcodec/assdec.c b/libavcodec/assdec.c index 5a51703e86..d79065663b 100644 --- a/libavcodec/assdec.c +++ b/libavcodec/assdec.c @@ -29,10 +29,11 @@ static av_cold int ass_decode_init(AVCodecContext *avctx) { - avctx->subtitle_header = av_malloc(avctx->extradata_size); + avctx->subtitle_header = av_malloc(avctx->extradata_size + 1); if (!avctx->subtitle_header) return AVERROR(ENOMEM); memcpy(avctx->subtitle_header, avctx->extradata, avctx->extradata_size); + avctx->subtitle_header[avctx->extradata_size] = 0; avctx->subtitle_header_size = avctx->extradata_size; avctx->priv_data = ff_ass_split(avctx->extradata); if(!avctx->priv_data) diff --git a/libavcodec/assenc.c b/libavcodec/assenc.c index 7ed21eedbc..50b89c00d6 100644 --- a/libavcodec/assenc.c +++ b/libavcodec/assenc.c @@ -28,11 +28,12 @@ static av_cold int ass_encode_init(AVCodecContext *avctx) { - avctx->extradata = av_malloc(avctx->subtitle_header_size); + avctx->extradata = av_malloc(avctx->subtitle_header_size + 1); if (!avctx->extradata) return AVERROR(ENOMEM); memcpy(avctx->extradata, avctx->subtitle_header, avctx->subtitle_header_size); avctx->extradata_size = avctx->subtitle_header_size; + avctx->extradata[avctx->extradata_size] = 0; return 0; } diff --git a/libavcodec/dvdsubenc.c b/libavcodec/dvdsubenc.c index 2e0c37da4d..352272d311 100644 --- a/libavcodec/dvdsubenc.c +++ b/libavcodec/dvdsubenc.c @@ -20,6 +20,7 @@ */ #include "avcodec.h" #include "bytestream.h" +#include "internal.h" #include "libavutil/avassert.h" #include "libavutil/bprint.h" #include "libavutil/imgutils.h" @@ -408,9 +409,9 @@ static int dvdsub_init(AVCodecContext *avctx) av_bprintf(&extradata, " %06"PRIx32"%c", dvdc->global_palette[i] & 0xFFFFFF, i < 15 ? ',' : '\n'); - if ((ret = av_bprint_finalize(&extradata, (char **)&avctx->extradata)) < 0) + ret = ff_bprint_to_extradata(avctx, &extradata); + if (ret < 0) return ret; - avctx->extradata_size = extradata.len; return 0; } diff --git a/libavcodec/internal.h b/libavcodec/internal.h index 2d3433f434..386f6f3c26 100644 --- a/libavcodec/internal.h +++ b/libavcodec/internal.h @@ -201,4 +201,9 @@ int ff_codec_open2_recursive(AVCodecContext *avctx, const AVCodec *codec, AVDict */ int ff_codec_close_recursive(AVCodecContext *avctx); +/** + * Finalize buf into extradata and set its size appropriately. + */ +int ff_bprint_to_extradata(AVCodecContext *avctx, struct AVBPrint *buf); + #endif /* AVCODEC_INTERNAL_H */ diff --git a/libavcodec/utils.c b/libavcodec/utils.c index b3669950d5..a275899ff7 100644 --- a/libavcodec/utils.c +++ b/libavcodec/utils.c @@ -27,6 +27,7 @@ #include "libavutil/avassert.h" #include "libavutil/avstring.h" +#include "libavutil/bprint.h" #include "libavutil/channel_layout.h" #include "libavutil/crc.h" #include "libavutil/mathematics.h" @@ -2684,3 +2685,21 @@ int avcodec_is_open(AVCodecContext *s) { return !!s->internal; } + +int ff_bprint_to_extradata(AVCodecContext *avctx, struct AVBPrint *buf) +{ + int ret; + char *str; + + ret = av_bprint_finalize(buf, &str); + if (ret < 0) + return ret; + avctx->extradata = str; + /* Note: the string is NUL terminated (so extradata can be read as a + * string), but the ending character is not accounted in the size (in + * binary formats you are likely not supposed to mux that character). When + * extradata is copied, it is also padded with FF_INPUT_BUFFER_PADDING_SIZE + * zeros. */ + avctx->extradata_size = buf->len; + return 0; +} |