Skip to content

Commit

Permalink
lavc: add ff_bprint_to_extradata() helper and use it.
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
ubitux committed Dec 30, 2012
1 parent e911f4a commit 36e61e2
Show file tree
Hide file tree
Showing 9 changed files with 45 additions and 23 deletions.
3 changes: 2 additions & 1 deletion libavcodec/assdec.c
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
3 changes: 2 additions & 1 deletion libavcodec/assenc.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand Down
5 changes: 3 additions & 2 deletions libavcodec/dvdsubenc.c
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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;
}
Expand Down
5 changes: 5 additions & 0 deletions libavcodec/internal.h
Original file line number Diff line number Diff line change
Expand Up @@ -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 */
19 changes: 19 additions & 0 deletions libavcodec/utils.c
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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;
}
8 changes: 3 additions & 5 deletions libavformat/assdec.c
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
#include "avformat.h"
#include "internal.h"
#include "subtitles.h"
#include "libavcodec/internal.h"
#include "libavutil/bprint.h"

typedef struct ASSContext{
Expand Down Expand Up @@ -132,12 +133,9 @@ static int ass_read_header(AVFormatContext *s)

av_bprint_finalize(&line, NULL);

av_bprint_finalize(&header, (char **)&st->codec->extradata);
if (!st->codec->extradata) {
res = AVERROR(ENOMEM);
res = ff_bprint_to_extradata(st->codec, &header);
if (res < 0)
goto end;
}
st->codec->extradata_size = header.len + 1;

ff_subtitles_queue_finalize(&ass->q);

Expand Down
8 changes: 5 additions & 3 deletions libavformat/jacosubdec.c
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
#include "avformat.h"
#include "internal.h"
#include "subtitles.h"
#include "libavcodec/internal.h"
#include "libavcodec/jacosub.h"
#include "libavutil/avstring.h"
#include "libavutil/bprint.h"
Expand Down Expand Up @@ -159,7 +160,7 @@ static int jacosub_read_header(AVFormatContext *s)
JACOsubContext *jacosub = s->priv_data;
int shift_set = 0; // only the first shift matters
int merge_line = 0;
int i;
int i, ret;

AVStream *st = avformat_new_stream(s, NULL);
if (!st)
Expand Down Expand Up @@ -228,8 +229,9 @@ static int jacosub_read_header(AVFormatContext *s)
}

/* general/essential directives in the extradata */
av_bprint_finalize(&header, (char **)&st->codec->extradata);
st->codec->extradata_size = header.len + 1;
ret = ff_bprint_to_extradata(st->codec, &header);
if (ret < 0)
return ret;

/* SHIFT and TIMERES affect the whole script so packet timing can only be
* done in a second pass */
Expand Down
9 changes: 3 additions & 6 deletions libavformat/samidec.c
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
#include "avformat.h"
#include "internal.h"
#include "subtitles.h"
#include "libavcodec/internal.h"
#include "libavutil/avstring.h"
#include "libavutil/bprint.h"
#include "libavutil/intreadwrite.h"
Expand Down Expand Up @@ -91,13 +92,9 @@ static int sami_read_header(AVFormatContext *s)
av_bprint_clear(&buf);
}

st->codec->extradata_size = hdr_buf.len + 1;
av_bprint_finalize(&hdr_buf, (char **)&st->codec->extradata);
if (!st->codec->extradata) {
st->codec->extradata_size = 0;
res = AVERROR(ENOMEM);
res = ff_bprint_to_extradata(st->codec, &hdr_buf);
if (res < 0)
goto end;
}

ff_subtitles_queue_finalize(&sami->q);

Expand Down
8 changes: 3 additions & 5 deletions libavformat/subviewerdec.c
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
#include "avformat.h"
#include "internal.h"
#include "subtitles.h"
#include "libavcodec/internal.h"
#include "libavutil/avstring.h"
#include "libavutil/bprint.h"
#include "libavutil/intreadwrite.h"
Expand Down Expand Up @@ -99,12 +100,9 @@ static int subviewer_read_header(AVFormatContext *s)
av_bprintf(&header, "%s", line);
if (!strncmp(line, "[END INFORMATION]", 17) || !strncmp(line, "[SUBTITLE]", 10)) {
/* end of header */
av_bprint_finalize(&header, (char **)&st->codec->extradata);
if (!st->codec->extradata) {
res = AVERROR(ENOMEM);
res = ff_bprint_to_extradata(st->codec, &header);
if (res < 0)
goto end;
}
st->codec->extradata_size = header.len + 1;
} else if (strncmp(line, "[INFORMATION]", 13)) {
/* assume file metadata at this point */
int i, j = 0;
Expand Down

0 comments on commit 36e61e2

Please sign in to comment.