Showing the changes to libavformat (1 of 1 file). Show all
Unified · Split
| @@ -132,7 +132,7 @@ typedef struct FLVContext { · 131 unmodified lines | |||
| 132 | int flags; | 132 | int flags; |
| 133 | int64_t *last_ts; | 133 | int64_t *last_ts; |
| 134 | int *metadata_pkt_written; | 134 | int *metadata_pkt_written; |
| 135 | int *video_track_idx_map; | 135 | int *track_idx_map; |
| 136 | } FLVContext; | 136 | } FLVContext; |
| 137 | 137 | ||
| 138 | static int get_audio_flags(AVFormatContext *s, AVCodecParameters *par) | 138 | static int get_audio_flags(AVFormatContext *s, AVCodecParameters *par) |
| @@ -765,19 +765,33 @@ static int flv_get_multichannel_body_size(AVCodecParameters* par) · 626 unmodified lines | |||
| 765 | return res; | 765 | return res; |
| 766 | } | 766 | } |
| 767 | 767 | ||
| 768 | static void flv_write_multichannel_header(AVFormatContext* s, AVCodecParameters* par, int64_t ts) | 768 | static void flv_write_multichannel_header(AVFormatContext* s, AVCodecParameters* par, int64_t ts, int stream_index) |
| 769 | { | 769 | { |
| 770 | AVIOContext *pb = s->pb; | 770 | AVIOContext *pb = s->pb; |
| 771 | FLVContext *flv = s->priv_data; | ||
| 772 | |||
| 773 | int track_idx = flv->track_idx_map[stream_index]; | ||
| 771 | int data_size = flv_get_multichannel_body_size(par); | 774 | int data_size = flv_get_multichannel_body_size(par); |
| 775 | if (track_idx) | ||
| 776 | data_size += 2; | ||
| 772 | 777 | ||
| 773 | avio_w8(pb, FLV_TAG_TYPE_AUDIO); | 778 | avio_w8(pb, FLV_TAG_TYPE_AUDIO); |
| 774 | avio_wb24(pb, data_size + 5); // size | 779 | avio_wb24(pb, data_size + 5); // size |
| 775 | put_timestamp(pb, ts); | 780 | put_timestamp(pb, ts); |
| 776 | avio_wb24(pb, 0); // streamid | 781 | avio_wb24(pb, 0); // streamid |
| 777 | 782 | ||
| 778 | avio_w8(pb, FLV_CODECID_EX_HEADER | AudioPacketTypeMultichannelConfig); | 783 | if (track_idx) { |
| 784 | avio_w8(pb, FLV_CODECID_EX_HEADER | AudioPacketTypeMultitrack); | ||
| 785 | avio_w8(pb, MultitrackTypeOneTrack | AudioPacketTypeMultichannelConfig); | ||
| 786 | } else { | ||
| 787 | avio_w8(pb, FLV_CODECID_EX_HEADER | AudioPacketTypeMultichannelConfig); | ||
| 788 | } | ||
| 789 | |||
| 779 | write_codec_fourcc(pb, par->codec_id); | 790 | write_codec_fourcc(pb, par->codec_id); |
| 780 | 791 | ||
| 792 | if (track_idx) | ||
| 793 | avio_w8(pb, track_idx); | ||
| 794 | |||
| 781 | flv_write_multichannel_body(s, par); | 795 | flv_write_multichannel_body(s, par); |
| 782 | 796 | ||
| 783 | avio_wb32(pb, data_size + 5 + 11); // previous tag size | 797 | avio_wb32(pb, data_size + 5 + 11); // previous tag size |
| @@ -787,6 +801,7 @@ static void flv_write_codec_header(AVFormatContext* s, AVCodecParameters* par, i · 3 unmodified lines | |||
| 787 | int64_t data_size; | 801 | int64_t data_size; |
| 788 | AVIOContext *pb = s->pb; | 802 | AVIOContext *pb = s->pb; |
| 789 | FLVContext *flv = s->priv_data; | 803 | FLVContext *flv = s->priv_data; |
| 804 | int track_idx = flv->track_idx_map[stream_index]; | ||
| 790 | int extended_flv = 0; | 805 | int extended_flv = 0; |
| 791 | 806 | ||
| 792 | if (par->codec_id == AV_CODEC_ID_AAC || par->codec_id == AV_CODEC_ID_H264 | 807 | if (par->codec_id == AV_CODEC_ID_AAC || par->codec_id == AV_CODEC_ID_H264 |
| @@ -803,15 +818,26 @@ static void flv_write_codec_header(AVFormatContext* s, AVCodecParameters* par, i · 10 unmodified lines | |||
| 803 | avio_wb24(pb, 0); // streamid | 818 | avio_wb24(pb, 0); // streamid |
| 804 | pos = avio_tell(pb); | 819 | pos = avio_tell(pb); |
| 805 | if (par->codec_type == AVMEDIA_TYPE_AUDIO) { | 820 | if (par->codec_type == AVMEDIA_TYPE_AUDIO) { |
| 806 | extended_flv = par->codec_id == AV_CODEC_ID_OPUS | 821 | extended_flv = (par->codec_id == AV_CODEC_ID_AAC && track_idx) |
| 807 | || par->codec_id == AV_CODEC_ID_FLAC | 822 | || (par->codec_id == AV_CODEC_ID_MP3 && track_idx) |
| 808 | || par->codec_id == AV_CODEC_ID_AC3 | 823 | || par->codec_id == AV_CODEC_ID_OPUS |
| 809 | || par->codec_id == AV_CODEC_ID_EAC3; | 824 | || par->codec_id == AV_CODEC_ID_FLAC |
| 825 | || par->codec_id == AV_CODEC_ID_AC3 | ||
| 826 | || par->codec_id == AV_CODEC_ID_EAC3; | ||
| 810 | 827 | ||
| 811 | if (extended_flv) { | 828 | if (extended_flv) { |
| 812 | avio_w8(pb, FLV_CODECID_EX_HEADER | AudioPacketTypeSequenceStart); | 829 | if (track_idx) { |
| 830 | avio_w8(pb, FLV_CODECID_EX_HEADER | AudioPacketTypeMultitrack); | ||
| 831 | avio_w8(pb, MultitrackTypeOneTrack | AudioPacketTypeSequenceStart); | ||
| 832 | } else { | ||
| 833 | avio_w8(pb, FLV_CODECID_EX_HEADER | AudioPacketTypeSequenceStart); | ||
| 834 | } | ||
| 835 | |||
| 813 | write_codec_fourcc(pb, par->codec_id); | 836 | write_codec_fourcc(pb, par->codec_id); |
| 814 | 837 | ||
| 838 | if (track_idx) | ||
| 839 | avio_w8(pb, track_idx); | ||
| 840 | |||
| 815 | if (par->codec_id == AV_CODEC_ID_AAC) { | 841 | if (par->codec_id == AV_CODEC_ID_AAC) { |
| 816 | flv_write_aac_header(s, par); | 842 | flv_write_aac_header(s, par); |
| 817 | } else if (par->codec_id == AV_CODEC_ID_OPUS || par->codec_id == AV_CODEC_ID_FLAC) { | 843 | } else if (par->codec_id == AV_CODEC_ID_OPUS || par->codec_id == AV_CODEC_ID_FLAC) { |
| @@ -825,7 +851,6 @@ static void flv_write_codec_header(AVFormatContext* s, AVCodecParameters* par, i · 7 unmodified lines | |||
| 825 | flv_write_aac_header(s, par); | 851 | flv_write_aac_header(s, par); |
| 826 | } | 852 | } |
| 827 | } else { | 853 | } else { |
| 828 | int track_idx = flv->video_track_idx_map[stream_index]; | ||
| 829 | // If video stream has track_idx > 0 we need to send H.264 as extended video packet | 854 | // If video stream has track_idx > 0 we need to send H.264 as extended video packet |
| 830 | extended_flv = (par->codec_id == AV_CODEC_ID_H264 && track_idx) || | 855 | extended_flv = (par->codec_id == AV_CODEC_ID_H264 && track_idx) || |
| 831 | par->codec_id == AV_CODEC_ID_HEVC || | 856 | par->codec_id == AV_CODEC_ID_HEVC || |
| @@ -869,7 +894,7 @@ static void flv_write_codec_header(AVFormatContext* s, AVCodecParameters* par, i · 37 unmodified lines | |||
| 869 | if (par->codec_type == AVMEDIA_TYPE_AUDIO && (extended_flv || | 894 | if (par->codec_type == AVMEDIA_TYPE_AUDIO && (extended_flv || |
| 870 | (av_channel_layout_compare(&par->ch_layout, &(AVChannelLayout)AV_CHANNEL_LAYOUT_STEREO) == 1 && | 895 | (av_channel_layout_compare(&par->ch_layout, &(AVChannelLayout)AV_CHANNEL_LAYOUT_STEREO) == 1 && |
| 871 | av_channel_layout_compare(&par->ch_layout, &(AVChannelLayout)AV_CHANNEL_LAYOUT_MONO) == 1))) | 896 | av_channel_layout_compare(&par->ch_layout, &(AVChannelLayout)AV_CHANNEL_LAYOUT_MONO) == 1))) |
| 872 | flv_write_multichannel_header(s, par, ts); | 897 | flv_write_multichannel_header(s, par, ts, stream_index); |
| 873 | } | 898 | } |
| 874 | 899 | ||
| 875 | static int flv_append_keyframe_info(AVFormatContext *s, FLVContext *flv, double ts, int64_t pos) | 900 | static int flv_append_keyframe_info(AVFormatContext *s, FLVContext *flv, double ts, int64_t pos) |
| @@ -931,13 +956,13 @@ static int shift_data(AVFormatContext *s) · 55 unmodified lines | |||
| 931 | static int flv_init(struct AVFormatContext *s) | 956 | static int flv_init(struct AVFormatContext *s) |
| 932 | { | 957 | { |
| 933 | int i; | 958 | int i; |
| 934 | int video_ctr = 0; | 959 | int video_ctr = 0, audio_ctr = 0; |
| 935 | FLVContext *flv = s->priv_data; | 960 | FLVContext *flv = s->priv_data; |
| 936 | 961 | ||
| 937 | flv->last_ts = av_calloc(s->nb_streams, sizeof(*flv->last_ts)); | 962 | flv->last_ts = av_calloc(s->nb_streams, sizeof(*flv->last_ts)); |
| 938 | flv->metadata_pkt_written = av_calloc(s->nb_streams, sizeof(*flv->metadata_pkt_written)); | 963 | flv->metadata_pkt_written = av_calloc(s->nb_streams, sizeof(*flv->metadata_pkt_written)); |
| 939 | flv->video_track_idx_map = av_calloc(s->nb_streams, sizeof(*flv->video_track_idx_map)); | 964 | flv->track_idx_map = av_calloc(s->nb_streams, sizeof(*flv->track_idx_map)); |
| 940 | if (!flv->last_ts || !flv->metadata_pkt_written || !flv->video_track_idx_map) | 965 | if (!flv->last_ts || !flv->metadata_pkt_written || !flv->track_idx_map) |
| 941 | return AVERROR(ENOMEM); | 966 | return AVERROR(ENOMEM); |
| 942 | 967 | ||
| 943 | for (i = 0; i < s->nb_streams; i++) { | 968 | for (i = 0; i < s->nb_streams; i++) { |
| @@ -949,7 +974,7 @@ static int flv_init(struct AVFormatContext *s) · 5 unmodified lines | |||
| 949 | s->streams[i]->avg_frame_rate.num) { | 974 | s->streams[i]->avg_frame_rate.num) { |
| 950 | flv->framerate = av_q2d(s->streams[i]->avg_frame_rate); | 975 | flv->framerate = av_q2d(s->streams[i]->avg_frame_rate); |
| 951 | } | 976 | } |
| 952 | flv->video_track_idx_map[i] = video_ctr++; | 977 | flv->track_idx_map[i] = video_ctr++; |
| 953 | if (flv->video_par && flv->flags & FLV_ADD_KEYFRAME_INDEX) { | 978 | if (flv->video_par && flv->flags & FLV_ADD_KEYFRAME_INDEX) { |
| 954 | av_log(s, AV_LOG_ERROR, | 979 | av_log(s, AV_LOG_ERROR, |
| 955 | "at most one video stream is supported in flv with keyframe index\n"); | 980 | "at most one video stream is supported in flv with keyframe index\n"); |
| @@ -980,12 +1005,22 @@ static int flv_init(struct AVFormatContext *s) · 24 unmodified lines | |||
| 980 | } | 1005 | } |
| 981 | break; | 1006 | break; |
| 982 | case AVMEDIA_TYPE_AUDIO: | 1007 | case AVMEDIA_TYPE_AUDIO: |
| 983 | if (flv->audio_par) { | 1008 | if (audio_ctr && |
| 984 | av_log(s, AV_LOG_ERROR, | 1009 | par->codec_id != AV_CODEC_ID_AAC && |
| 985 | "at most one audio stream is supported in flv\n"); | 1010 | par->codec_id != AV_CODEC_ID_MP3 && |
| 1011 | par->codec_id != AV_CODEC_ID_OPUS && | ||
| 1012 | par->codec_id != AV_CODEC_ID_FLAC && | ||
| 1013 | par->codec_id != AV_CODEC_ID_AC3 && | ||
| 1014 | par->codec_id != AV_CODEC_ID_EAC3) { | ||
| 1015 | av_log(s, AV_LOG_ERROR, "Unsupported multi-track codec.\n"); | ||
| 986 | return AVERROR(EINVAL); | 1016 | return AVERROR(EINVAL); |
| 987 | } | 1017 | } |
| 988 | flv->audio_par = par; | 1018 | flv->track_idx_map[i] = audio_ctr++; |
| 1019 | if (flv->audio_par) | ||
| 1020 | av_log(s, AV_LOG_WARNING, | ||
| 1021 | "more than one audio stream is not supported by most flv demuxers.\n"); | ||
| 1022 | else | ||
| 1023 | flv->audio_par = par; | ||
| 989 | if (get_audio_flags(s, par) < 0) | 1024 | if (get_audio_flags(s, par) < 0) |
| 990 | return unsupported_codec(s, "Audio", par->codec_id); | 1025 | return unsupported_codec(s, "Audio", par->codec_id); |
| 991 | if (par->codec_id == AV_CODEC_ID_PCM_S16BE) | 1026 | if (par->codec_id == AV_CODEC_ID_PCM_S16BE) |
| @@ -1157,9 +1192,11 @@ static int flv_write_packet(AVFormatContext *s, AVPacket *pkt) · 165 unmodified lines | |||
| 1157 | uint8_t frametype = pkt->flags & AV_PKT_FLAG_KEY ? FLV_FRAME_KEY : FLV_FRAME_INTER; | 1192 | uint8_t frametype = pkt->flags & AV_PKT_FLAG_KEY ? FLV_FRAME_KEY : FLV_FRAME_INTER; |
| 1158 | int flags = -1, flags_size, ret = 0; | 1193 | int flags = -1, flags_size, ret = 0; |
| 1159 | int64_t cur_offset = avio_tell(pb); | 1194 | int64_t cur_offset = avio_tell(pb); |
| 1160 | int track_idx = flv->video_track_idx_map[pkt->stream_index]; | 1195 | int track_idx = flv->track_idx_map[pkt->stream_index]; |
| 1161 | 1196 | ||
| 1162 | int extended_audio = par->codec_id == AV_CODEC_ID_OPUS | 1197 | int extended_audio = (par->codec_id == AV_CODEC_ID_AAC && track_idx) |
| 1198 | || (par->codec_id == AV_CODEC_ID_MP3 && track_idx) | ||
| 1199 | || par->codec_id == AV_CODEC_ID_OPUS | ||
| 1163 | || par->codec_id == AV_CODEC_ID_FLAC | 1200 | || par->codec_id == AV_CODEC_ID_FLAC |
| 1164 | || par->codec_id == AV_CODEC_ID_AC3 | 1201 | || par->codec_id == AV_CODEC_ID_AC3 |
| 1165 | || par->codec_id == AV_CODEC_ID_EAC3; | 1202 | || par->codec_id == AV_CODEC_ID_EAC3; |
| @@ -1176,8 +1213,8 @@ static int flv_write_packet(AVFormatContext *s, AVPacket *pkt) · 10 unmodified lines | |||
| 1176 | else | 1213 | else |
| 1177 | flags_size = 1; | 1214 | flags_size = 1; |
| 1178 | 1215 | ||
| 1179 | if (par->codec_type == AVMEDIA_TYPE_VIDEO && track_idx) | 1216 | if ((par->codec_type == AVMEDIA_TYPE_VIDEO || par->codec_type == AVMEDIA_TYPE_AUDIO) && track_idx) |
| 1180 | flags_size += 2; // additional header bytes for multi-track video | 1217 | flags_size += 2; // additional header bytes for multi-track flv |
| 1181 | 1218 | ||
| 1182 | if ((par->codec_id == AV_CODEC_ID_HEVC || | 1219 | if ((par->codec_id == AV_CODEC_ID_HEVC || |
| 1183 | (par->codec_id == AV_CODEC_ID_H264 && track_idx)) | 1220 | (par->codec_id == AV_CODEC_ID_H264 && track_idx)) |
| @@ -1344,8 +1381,15 @@ static int flv_write_packet(AVFormatContext *s, AVPacket *pkt) · 160 unmodified lines | |||
| 1344 | if (h2645 && pkttype == PacketTypeCodedFrames) | 1381 | if (h2645 && pkttype == PacketTypeCodedFrames) |
| 1345 | avio_wb24(pb, pkt->pts - pkt->dts); | 1382 | avio_wb24(pb, pkt->pts - pkt->dts); |
| 1346 | } else if (extended_audio) { | 1383 | } else if (extended_audio) { |
| 1347 | avio_w8(pb, FLV_CODECID_EX_HEADER | AudioPacketTypeCodedFrames); | 1384 | if (track_idx) { |
| 1385 | avio_w8(pb, FLV_CODECID_EX_HEADER | AudioPacketTypeMultitrack); | ||
| 1386 | avio_w8(pb, MultitrackTypeOneTrack | AudioPacketTypeCodedFrames); | ||
| 1387 | } else { | ||
| 1388 | avio_w8(pb, FLV_CODECID_EX_HEADER | AudioPacketTypeCodedFrames); | ||
| 1389 | } | ||
| 1348 | write_codec_fourcc(pb, par->codec_id); | 1390 | write_codec_fourcc(pb, par->codec_id); |
| 1391 | if (track_idx) | ||
| 1392 | avio_w8(pb, track_idx); | ||
| 1349 | } else { | 1393 | } else { |
| 1350 | av_assert1(flags >= 0); | 1394 | av_assert1(flags >= 0); |
| 1351 | avio_w8(pb, flags); | 1395 | avio_w8(pb, flags); |
| @@ -1435,7 +1479,7 @@ static void flv_deinit(AVFormatContext *s) · 83 unmodified lines | |||
| 1435 | 1479 | ||
| 1436 | av_freep(&flv->last_ts); | 1480 | av_freep(&flv->last_ts); |
| 1437 | av_freep(&flv->metadata_pkt_written); | 1481 | av_freep(&flv->metadata_pkt_written); |
| 1438 | av_freep(&flv->video_track_idx_map); | 1482 | av_freep(&flv->track_idx_map); |
| 1439 | } | 1483 | } |
| 1440 | 1484 | ||
| 1441 | static const AVOption options[] = { | 1485 | static const AVOption options[] = { |