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; int64_t file_size; if (pb->seekable) { if (avi->riff_id == 1) { ff_end_tag(pb, avi->movi_list); res = avi_write_idx1(s); ff_end_tag(pb, avi->riff_start); } else { avi_write_ix(s); ff_end_tag(pb, avi->movi_list); ff_end_tag(pb, avi->riff_start); file_size = avio_tell(pb); avio_seek(pb, avi->odml_list - 8, SEEK_SET); ffio_wfourcc(pb, "LIST"); /* Making this AVI OpenDML one */ avio_skip(pb, 16); for (n = nb_frames = 0; n < s->nb_streams; n++) { AVCodecContext *stream = s->streams[n]->codec; AVIStream *avist = s->streams[n]->priv_data; if (stream->codec_type == AVMEDIA_TYPE_VIDEO) { if (nb_frames < avist->packet_count) nb_frames = avist->packet_count; } else { if (stream->codec_id == AV_CODEC_ID_MP2 || stream->codec_id == AV_CODEC_ID_MP3) nb_frames += avist->packet_count; } } avio_wl32(pb, nb_frames); avio_seek(pb, file_size, SEEK_SET); avi_write_counters(s, avi->riff_id); } } 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_seek(pb, avist->frames_hdr_strm + 4, SEEK_SET); avio_wl32(pb, avist->max_size); } } return res; }
static int avi_write_trailer(AVFormatContext *s) { AVIContext *avi = s->priv_data; ByteIOContext *pb = &s->pb; int res = 0; int i, j, n, nb_frames; offset_t file_size; if (!url_is_streamed(pb)) { if (avi->riff_id == 1) { end_tag(pb, avi->movi_list); res = avi_write_idx1(s); end_tag(pb, avi->riff_start); } else { avi_write_ix(s); end_tag(pb, avi->movi_list); end_tag(pb, avi->riff_start); file_size = url_ftell(pb); url_fseek(pb, avi->odml_list - 8, SEEK_SET); put_tag(pb, "LIST"); /* Making this AVI OpenDML one */ url_fskip(pb, 16); for (n=nb_frames=0;n<s->nb_streams;n++) { AVCodecContext *stream = s->streams[n]->codec; if (stream->codec_type == CODEC_TYPE_VIDEO) { if (nb_frames < avi->packet_count[n]) nb_frames = avi->packet_count[n]; } else { if (stream->codec_id == CODEC_ID_MP2 || stream->codec_id == CODEC_ID_MP3) { nb_frames += avi->packet_count[n]; } } } put_le32(pb, nb_frames); url_fseek(pb, file_size, SEEK_SET); avi_write_counters(s, avi->riff_id); } } put_flush_packet(pb); for (i=0; i<MAX_STREAMS; i++) { for (j=0; j<avi->indexes[i].ents_allocated/AVI_INDEX_CLUSTER_SIZE; j++) av_free(avi->indexes[i].cluster[j]); av_free(avi->indexes[i].cluster); avi->indexes[i].cluster = NULL; avi->indexes[i].ents_allocated = avi->indexes[i].entry = 0; } return res; }
int avi_close(avi_Context* AVI) { int res = 0; int n, nb_frames; int64_t file_size; avi_RIFF* riff = avi_get_last_riff(AVI); if (riff->id == 1) { avi_close_tag(AVI, riff->movi_list); fprintf(stderr, "AVI: (%" PRIu64 ") close movi tag\n",io_get_offset(AVI->writer)); res = avi_write_idx1(AVI, riff); avi_close_tag(AVI, riff->riff_start); } else { avi_write_ix(AVI); avi_close_tag(AVI, riff->movi_list); avi_close_tag(AVI, riff->riff_start); file_size = io_get_offset(AVI->writer); io_seek(AVI->writer, AVI->odml_list - 8); io_write_4cc(AVI->writer, "LIST"); /* Making this AVI OpenDML one */ io_skip(AVI->writer, 16); for (n=nb_frames=0;n<AVI->stream_list_size;n++) { io_Stream *stream = get_stream(AVI->stream_list, n); if (stream->type == STREAM_TYPE_VIDEO) { if (nb_frames < stream->packet_count) nb_frames = stream->packet_count; } else { if (stream->codec_id == CODEC_ID_MP2 || stream->codec_id == CODEC_ID_MP3) nb_frames += stream->packet_count; } } io_write_wl32(AVI->writer, nb_frames); io_seek(AVI->writer, file_size); avi_write_counters(AVI, riff); } clean_indexes(AVI); return res; }
static int avi_write_packet(AVFormatContext *s, AVPacket *pkt) { AVIContext *avi = s->priv_data; AVIOContext *pb = s->pb; unsigned char tag[5]; unsigned int flags=0; const int stream_index= pkt->stream_index; AVIStream *avist= s->streams[stream_index]->priv_data; AVCodecContext *enc= s->streams[stream_index]->codec; int size= pkt->size; av_dlog(s, "dts:%s packet_count:%d stream_index:%d\n", av_ts2str(pkt->dts), avist->packet_count, stream_index); while(enc->block_align==0 && pkt->dts != AV_NOPTS_VALUE && pkt->dts > avist->packet_count && enc->codec_id != AV_CODEC_ID_XSUB && avist->packet_count){ AVPacket empty_packet; if(pkt->dts - avist->packet_count > 60000){ av_log(s, AV_LOG_ERROR, "Too large number of skipped frames %"PRId64" > 60000\n", pkt->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(s, &empty_packet); av_dlog(s, "dup dts:%s packet_count:%d\n", av_ts2str(pkt->dts), avist->packet_count); } avist->packet_count++; // Make sure to put an OpenDML chunk when the file size exceeds the limits if (pb->seekable && (avio_tell(pb) - avi->riff_start > AVI_MAX_RIFF_SIZE)) { avi_write_ix(s); ff_end_tag(pb, avi->movi_list); if (avi->riff_id == 1) avi_write_idx1(s); ff_end_tag(pb, avi->riff_start); avi->movi_list = avi_start_new_riff(s, pb, "AVIX", "movi"); } avi_stream2fourcc(tag, stream_index, enc->codec_type); if(pkt->flags&AV_PKT_FLAG_KEY) flags = 0x10; if (enc->codec_type == AVMEDIA_TYPE_AUDIO) { avist->audio_strm_length += size; } if (s->pb->seekable) { AVIIndex* idx = &avist->indexes; int cl = idx->entry / AVI_INDEX_CLUSTER_SIZE; int id = idx->entry % AVI_INDEX_CLUSTER_SIZE; if (idx->ents_allocated <= idx->entry) { idx->cluster = av_realloc_f(idx->cluster, sizeof(void*), cl+1); if (!idx->cluster) { idx->ents_allocated = 0; idx->entry = 0; return AVERROR(ENOMEM); } idx->cluster[cl] = av_malloc(AVI_INDEX_CLUSTER_SIZE*sizeof(AVIIentry)); if (!idx->cluster[cl]) return AVERROR(ENOMEM); idx->ents_allocated += AVI_INDEX_CLUSTER_SIZE; } idx->cluster[cl][id].flags = flags; idx->cluster[cl][id].pos = avio_tell(pb) - avi->movi_list; idx->cluster[cl][id].len = size; idx->entry++; } avio_write(pb, tag, 4); avio_wl32(pb, size); avio_write(pb, pkt->data, size); if (size & 1) avio_w8(pb, 0); return 0; }
static int avi_write_packet(AVFormatContext *s, AVPacket *pkt) { AVIContext *avi = s->priv_data; ByteIOContext *pb = &s->pb; unsigned char tag[5]; unsigned int flags=0; const int stream_index= pkt->stream_index; AVCodecContext *enc= s->streams[stream_index]->codec; int size= pkt->size; // av_log(s, AV_LOG_DEBUG, "%"PRId64" %d %d\n", pkt->dts, avi->packet_count[stream_index], stream_index); while(enc->block_align==0 && pkt->dts != AV_NOPTS_VALUE && pkt->dts > avi->packet_count[stream_index]){ AVPacket empty_packet; av_init_packet(&empty_packet); empty_packet.size= 0; empty_packet.data= NULL; empty_packet.stream_index= stream_index; avi_write_packet(s, &empty_packet); // av_log(s, AV_LOG_DEBUG, "dup %"PRId64" %d\n", pkt->dts, avi->packet_count[stream_index]); } avi->packet_count[stream_index]++; // Make sure to put an OpenDML chunk when the file size exceeds the limits if (!url_is_streamed(pb) && (url_ftell(pb) - avi->riff_start > AVI_MAX_RIFF_SIZE)) { avi_write_ix(s); end_tag(pb, avi->movi_list); if (avi->riff_id == 1) avi_write_idx1(s); end_tag(pb, avi->riff_start); avi->movi_list = avi_start_new_riff(avi, pb, "AVIX", "movi"); } avi_stream2fourcc(&tag[0], stream_index, enc->codec_type); if(pkt->flags&PKT_FLAG_KEY) flags = 0x10; if (enc->codec_type == CODEC_TYPE_AUDIO) { avi->audio_strm_length[stream_index] += size; } if (!url_is_streamed(&s->pb)) { AVIIndex* idx = &avi->indexes[stream_index]; int cl = idx->entry / AVI_INDEX_CLUSTER_SIZE; int id = idx->entry % AVI_INDEX_CLUSTER_SIZE; if (idx->ents_allocated <= idx->entry) { idx->cluster = av_realloc(idx->cluster, (cl+1)*sizeof(void*)); if (!idx->cluster) return -1; idx->cluster[cl] = av_malloc(AVI_INDEX_CLUSTER_SIZE*sizeof(AVIIentry)); if (!idx->cluster[cl]) return -1; idx->ents_allocated += AVI_INDEX_CLUSTER_SIZE; } idx->cluster[cl][id].flags = flags; idx->cluster[cl][id].pos = url_ftell(pb) - avi->movi_list; idx->cluster[cl][id].len = size; idx->entry++; } put_buffer(pb, tag, 4); put_le32(pb, size); put_buffer(pb, pkt->data, size); if (size & 1) put_byte(pb, 0); put_flush_packet(pb); return 0; }
static int avi_write_packet(AVFormatContext *s, AVPacket *pkt) { unsigned char tag[5]; unsigned int flags = 0; const int stream_index = pkt->stream_index; int size = pkt->size; AVIContext *avi = s->priv_data; AVIOContext *pb = s->pb; AVIStream *avist = s->streams[stream_index]->priv_data; AVCodecContext *enc = s->streams[stream_index]->codec; int ret; if (enc->codec_id == AV_CODEC_ID_H264 && enc->codec_tag == MKTAG('H','2','6','4') && pkt->size) { ret = ff_check_h264_startcode(s, s->streams[stream_index], pkt); if (ret < 0) return ret; } if ((ret = write_skip_frames(s, stream_index, pkt->dts)) < 0) return ret; if (pkt->dts != AV_NOPTS_VALUE) avist->last_dts = pkt->dts + pkt->duration; avist->packet_count++; // Make sure to put an OpenDML chunk when the file size exceeds the limits if (pb->seekable && (avio_tell(pb) - avi->riff_start > AVI_MAX_RIFF_SIZE)) { avi_write_ix(s); ff_end_tag(pb, avi->movi_list); if (avi->riff_id == 1) avi_write_idx1(s); ff_end_tag(pb, avi->riff_start); avi->movi_list = avi_start_new_riff(s, pb, "AVIX", "movi"); } avi_stream2fourcc(tag, stream_index, enc->codec_type); if (pkt->flags & AV_PKT_FLAG_KEY) flags = 0x10; if (enc->codec_type == AVMEDIA_TYPE_AUDIO) avist->audio_strm_length += size; if (s->pb->seekable) { AVIIndex *idx = &avist->indexes; int cl = idx->entry / AVI_INDEX_CLUSTER_SIZE; int id = idx->entry % AVI_INDEX_CLUSTER_SIZE; if (idx->ents_allocated <= idx->entry) { idx->cluster = av_realloc_f(idx->cluster, sizeof(void*), cl+1); if (!idx->cluster) { idx->ents_allocated = 0; idx->entry = 0; return AVERROR(ENOMEM); } idx->cluster[cl] = av_malloc(AVI_INDEX_CLUSTER_SIZE * sizeof(AVIIentry)); if (!idx->cluster[cl]) return AVERROR(ENOMEM); idx->ents_allocated += AVI_INDEX_CLUSTER_SIZE; } idx->cluster[cl][id].flags = flags; idx->cluster[cl][id].pos = avio_tell(pb) - avi->movi_list; idx->cluster[cl][id].len = size; avist->max_size = FFMAX(avist->max_size, size); idx->entry++; } avio_write(pb, tag, 4); avio_wl32(pb, size); avio_write(pb, pkt->data, size); if (size & 1) avio_w8(pb, 0); return 0; }
int avi_write_packet(avi_Context* AVI, int stream_index, BYTE *data, uint32_t size, int64_t dts, int block_align, int32_t flags) { char tag[5]; unsigned int i_flags=0; io_Stream *stream= get_stream(AVI->stream_list, stream_index); avi_RIFF* riff = avi_get_last_riff(AVI); //align while(block_align==0 && dts != AV_NOPTS_VALUE && dts > stream->packet_count) avi_write_packet(AVI, stream_index, NULL, 0, AV_NOPTS_VALUE, 0, 0); stream->packet_count++; // Make sure to put an OpenDML chunk when the file size exceeds the limits if (io_get_offset(AVI->writer) - riff->riff_start > AVI_MAX_RIFF_SIZE) { avi_write_ix(AVI); avi_close_tag(AVI, riff->movi_list); if (riff->id == 1) avi_write_idx1(AVI, riff); avi_close_tag(AVI, riff->riff_start); avi_add_new_riff(AVI); riff = avi_get_last_riff(AVI); //update riff } avi_stream2fourcc(tag, stream); if(flags & AV_PKT_FLAG_KEY) //key frame i_flags = 0x10; if (stream->type == STREAM_TYPE_AUDIO) stream->audio_strm_length += size; avi_Index* idx = (avi_Index*) stream->indexes; int cl = idx->entry / AVI_INDEX_CLUSTER_SIZE; int id = idx->entry % AVI_INDEX_CLUSTER_SIZE; if (idx->ents_allocated <= idx->entry) { idx->cluster = av_realloc(idx->cluster, (cl+1)*sizeof(void*)); if (!idx->cluster) return -1; idx->cluster[cl] = av_malloc(AVI_INDEX_CLUSTER_SIZE*sizeof(avi_Ientry)); if (!idx->cluster[cl]) return -1; idx->ents_allocated += AVI_INDEX_CLUSTER_SIZE; } idx->cluster[cl][id].flags = i_flags; idx->cluster[cl][id].pos = io_get_offset(AVI->writer) - riff->movi_list; idx->cluster[cl][id].len = size; idx->entry++; io_write_4cc(AVI->writer, tag); io_write_wl32(AVI->writer, size); io_write_buf(AVI->writer, data, size); if (size & 1) io_write_w8(AVI->writer, 0); io_flush_buffer(AVI->writer); return 0; }
static int avi_write_packet(AVFormatContext *s, AVPacket *pkt) { unsigned char tag[5]; unsigned int flags = 0; const int stream_index = pkt->stream_index; int size = pkt->size; AVIContext *avi = s->priv_data; AVIOContext *pb = s->pb; AVIStream *avist = s->streams[stream_index]->priv_data; AVCodecContext *enc = s->streams[stream_index]->codec; while (enc->block_align == 0 && pkt->dts != AV_NOPTS_VALUE && pkt->dts > avist->packet_count) { AVPacket empty_packet; av_init_packet(&empty_packet); empty_packet.size = 0; empty_packet.data = NULL; empty_packet.stream_index = stream_index; avi_write_packet(s, &empty_packet); } avist->packet_count++; // Make sure to put an OpenDML chunk when the file size exceeds the limits if (pb->seekable && (avio_tell(pb) - avi->riff_start > AVI_MAX_RIFF_SIZE)) { avi_write_ix(s); ff_end_tag(pb, avi->movi_list); if (avi->riff_id == 1) avi_write_idx1(s); ff_end_tag(pb, avi->riff_start); avi->movi_list = avi_start_new_riff(s, pb, "AVIX", "movi"); } avi_stream2fourcc(tag, stream_index, enc->codec_type); if (pkt->flags & AV_PKT_FLAG_KEY) flags = 0x10; if (enc->codec_type == AVMEDIA_TYPE_AUDIO) avist->audio_strm_length += size; if (s->pb->seekable) { int err; AVIIndex *idx = &avist->indexes; int cl = idx->entry / AVI_INDEX_CLUSTER_SIZE; int id = idx->entry % AVI_INDEX_CLUSTER_SIZE; if (idx->ents_allocated <= idx->entry) { if ((err = av_reallocp(&idx->cluster, (cl + 1) * sizeof(*idx->cluster))) < 0) { idx->ents_allocated = 0; idx->entry = 0; return err; } idx->cluster[cl] = av_malloc(AVI_INDEX_CLUSTER_SIZE * sizeof(AVIIentry)); if (!idx->cluster[cl]) return -1; idx->ents_allocated += AVI_INDEX_CLUSTER_SIZE; } idx->cluster[cl][id].flags = flags; idx->cluster[cl][id].pos = avio_tell(pb) - avi->movi_list; idx->cluster[cl][id].len = size; idx->entry++; } avio_write(pb, tag, 4); avio_wl32(pb, size); avio_write(pb, pkt->data, size); if (size & 1) avio_w8(pb, 0); return 0; }