X-Git-Url: https://git.sesse.net/?a=blobdiff_plain;f=libavformat%2Favienc.c;h=ac11dd9cb233434a47e4cc3750bb0b75727fe995;hb=3a370868dc33061a20d1fd99274e65167d7a78ac;hp=ac0f04c3547fd83a51780ae39ceceeb2ca8c827f;hpb=b4ca32414ea28ad29b4bd387c298f5a676dace2a;p=ffmpeg diff --git a/libavformat/avienc.c b/libavformat/avienc.c index ac0f04c3547..ac11dd9cb23 100644 --- a/libavformat/avienc.c +++ b/libavformat/avienc.c @@ -31,7 +31,6 @@ #include "libavutil/avstring.h" #include "libavutil/avutil.h" #include "libavutil/internal.h" -#include "libavutil/intreadwrite.h" #include "libavutil/dict.h" #include "libavutil/avassert.h" #include "libavutil/timestamp.h" @@ -67,12 +66,14 @@ typedef struct AVIIndex { typedef struct AVIContext { const AVClass *class; + AVPacket *empty_packet; int64_t riff_start, movi_list, odml_list; int64_t frames_hdr_all; int riff_id; int reserve_index_space; int master_index_max_size; int write_channel_mask; + int flipped_raw_rgb; } AVIContext; typedef struct AVIStream { @@ -269,11 +270,15 @@ static int avi_write_header(AVFormatContext *s) int padding; if (s->nb_streams > AVI_MAX_STREAM_COUNT) { - av_log(s, AV_LOG_ERROR, "AVI does not support >%d streams\n", - AVI_MAX_STREAM_COUNT); + av_log(s, AV_LOG_ERROR, "AVI does not support " + ">"AV_STRINGIFY(AVI_MAX_STREAM_COUNT)" streams\n"); return AVERROR(EINVAL); } + avi->empty_packet = av_packet_alloc(); + if (!avi->empty_packet) + return AVERROR(ENOMEM); + for (n = 0; n < s->nb_streams; n++) { s->streams[n]->priv_data = av_mallocz(sizeof(AVIStream)); if (!s->streams[n]->priv_data) @@ -450,7 +455,7 @@ static int avi_write_header(AVFormatContext *s) && par->bits_per_coded_sample == 15) par->bits_per_coded_sample = 16; avist->pal_offset = avio_tell(pb) + 40; - ff_put_bmp_header(pb, par, 0, 0); + ff_put_bmp_header(pb, par, 0, 0, avi->flipped_raw_rgb); pix_fmt = avpriv_find_pix_fmt(avpriv_pix_fmt_bps_avi, par->bits_per_coded_sample); if ( !par->codec_tag @@ -459,6 +464,14 @@ static int avi_write_header(AVFormatContext *s) && par->format != AV_PIX_FMT_NONE) av_log(s, AV_LOG_ERROR, "%s rawvideo cannot be written to avi, output file will be unreadable\n", av_get_pix_fmt_name(par->format)); + + if (par->format == AV_PIX_FMT_PAL8) { + if (par->bits_per_coded_sample < 0 || par->bits_per_coded_sample > 8) { + av_log(s, AV_LOG_ERROR, "PAL8 with %d bps is not allowed\n", par->bits_per_coded_sample); + return AVERROR(EINVAL); + } + } + break; case AVMEDIA_TYPE_AUDIO: flags = (avi->write_channel_mask == 0) ? FF_PUT_WAV_HEADER_SKIP_CHANNELMASK : 0; @@ -581,8 +594,6 @@ static int avi_write_header(AVFormatContext *s) avi->movi_list = ff_start_tag(pb, "LIST"); ffio_wfourcc(pb, "movi"); - avio_flush(pb); - return 0; } @@ -594,7 +605,6 @@ static void update_odml_entry(AVFormatContext *s, int stream_index, int64_t ix, int64_t pos; int au_byterate, au_ssize, au_scale; - avio_flush(pb); pos = avio_tell(pb); /* Updating one entry in the AVI OpenDML master index */ @@ -734,24 +744,21 @@ static int avi_write_idx1(AVFormatContext *s) static int write_skip_frames(AVFormatContext *s, int stream_index, int64_t dts) { + AVIContext *avi = s->priv_data; AVIStream *avist = s->streams[stream_index]->priv_data; AVCodecParameters *par = s->streams[stream_index]->codecpar; ff_dlog(s, "dts:%s packet_count:%d stream_index:%d\n", av_ts2str(dts), avist->packet_count, stream_index); while (par->block_align == 0 && dts != AV_NOPTS_VALUE && dts > avist->packet_count && par->codec_id != AV_CODEC_ID_XSUB && avist->packet_count) { - AVPacket empty_packet; if (dts - avist->packet_count > 60000) { av_log(s, AV_LOG_ERROR, "Too large number of skipped frames %"PRId64" > 60000\n", dts - avist->packet_count); return AVERROR(EINVAL); } - av_init_packet(&empty_packet); - empty_packet.size = 0; - empty_packet.data = NULL; - empty_packet.stream_index = stream_index; - avi_write_packet_internal(s, &empty_packet); + avi->empty_packet->stream_index = stream_index; + avi_write_packet_internal(s, avi->empty_packet); ff_dlog(s, "dup dts:%s packet_count:%d\n", av_ts2str(dts), avist->packet_count); } @@ -909,7 +916,7 @@ static int avi_write_trailer(AVFormatContext *s) AVIContext *avi = s->priv_data; AVIOContext *pb = s->pb; int res = 0; - int i, j, n, nb_frames; + int i, n, nb_frames; int64_t file_size; for (i = 0; i < s->nb_streams; i++) { @@ -962,10 +969,6 @@ static int avi_write_trailer(AVFormatContext *s) for (i = 0; i < s->nb_streams; i++) { AVIStream *avist = s->streams[i]->priv_data; - for (j = 0; j < avist->indexes.ents_allocated / AVI_INDEX_CLUSTER_SIZE; j++) - av_freep(&avist->indexes.cluster[j]); - av_freep(&avist->indexes.cluster); - avist->indexes.ents_allocated = avist->indexes.entry = 0; if (pb->seekable & AVIO_SEEKABLE_NORMAL) { avio_seek(pb, avist->frames_hdr_strm + 4, SEEK_SET); avio_wl32(pb, avist->max_size); @@ -975,11 +978,29 @@ static int avi_write_trailer(AVFormatContext *s) return res; } +static void avi_deinit(AVFormatContext *s) +{ + AVIContext *avi = s->priv_data; + + av_packet_free(&avi->empty_packet); + + for (int i = 0; i < s->nb_streams; i++) { + AVIStream *avist = s->streams[i]->priv_data; + if (!avist) + continue; + for (int j = 0; j < avist->indexes.ents_allocated / AVI_INDEX_CLUSTER_SIZE; j++) + av_freep(&avist->indexes.cluster[j]); + av_freep(&avist->indexes.cluster); + avist->indexes.ents_allocated = avist->indexes.entry = 0; + } +} + #define OFFSET(x) offsetof(AVIContext, x) #define ENC AV_OPT_FLAG_ENCODING_PARAM static const AVOption options[] = { { "reserve_index_space", "reserve space (in bytes) at the beginning of the file for each stream index", OFFSET(reserve_index_space), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, INT_MAX, ENC }, { "write_channel_mask", "write channel mask into wave format header", OFFSET(write_channel_mask), AV_OPT_TYPE_BOOL, { .i64 = 1 }, 0, 1, ENC }, + { "flipped_raw_rgb", "Raw RGB bitmaps are stored bottom-up", OFFSET(flipped_raw_rgb), AV_OPT_TYPE_BOOL, { .i64 = 0 }, 0, 1, ENC }, { NULL }, }; @@ -999,11 +1020,10 @@ AVOutputFormat ff_avi_muxer = { .audio_codec = CONFIG_LIBMP3LAME ? AV_CODEC_ID_MP3 : AV_CODEC_ID_AC3, .video_codec = AV_CODEC_ID_MPEG4, .init = avi_init, + .deinit = avi_deinit, .write_header = avi_write_header, .write_packet = avi_write_packet, .write_trailer = avi_write_trailer, - .codec_tag = (const AVCodecTag * const []) { - ff_codec_bmp_tags, ff_codec_wav_tags, 0 - }, + .codec_tag = ff_riff_codec_tags_list, .priv_class = &avi_muxer_class, };