Showing the changes to libavformat (1 of 1 file). Show all
Unified · Split
| @@ -132,7 +132,7 @@ typedef struct FLVContext { · 131 unmodified lines | ||
| 132 | 132 | int flags; |
| 133 | 133 | int64_t *last_ts; |
| 134 | 134 | int *metadata_pkt_written; |
| 135 | − int *video_track_idx_map; | |
| 135 | + int *track_idx_map; | |
| 136 | 136 | } FLVContext; |
| 137 | 137 | |
| 138 | 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 | 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 | 770 | AVIOContext *pb = s->pb; |
| 771 | + FLVContext *flv = s->priv_data; | |
| 772 | + | |
| 773 | + int track_idx = flv->track_idx_map[stream_index]; | |
| 771 | 774 | int data_size = flv_get_multichannel_body_size(par); |
| 775 | + if (track_idx) | |
| 776 | + data_size += 2; | |
| 772 | 777 | |
| 773 | 778 | avio_w8(pb, FLV_TAG_TYPE_AUDIO); |
| 774 | 779 | avio_wb24(pb, data_size + 5); // size |
| 775 | 780 | put_timestamp(pb, ts); |
| 776 | 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 | 790 | write_codec_fourcc(pb, par->codec_id); |
| 780 | 791 | |
| 792 | + if (track_idx) | |
| 793 | + avio_w8(pb, track_idx); | |
| 794 | + | |
| 781 | 795 | flv_write_multichannel_body(s, par); |
| 782 | 796 | |
| 783 | 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 | 801 | int64_t data_size; |
| 788 | 802 | AVIOContext *pb = s->pb; |
| 789 | 803 | FLVContext *flv = s->priv_data; |
| 804 | + int track_idx = flv->track_idx_map[stream_index]; | |
| 790 | 805 | int extended_flv = 0; |
| 791 | 806 | |
| 792 | 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 | 818 | avio_wb24(pb, 0); // streamid |
| 804 | 819 | pos = avio_tell(pb); |
| 805 | 820 | if (par->codec_type == AVMEDIA_TYPE_AUDIO) { |
| 806 | − extended_flv = par->codec_id == AV_CODEC_ID_OPUS | |
| 807 | − || par->codec_id == AV_CODEC_ID_FLAC | |
| 808 | − || par->codec_id == AV_CODEC_ID_AC3 | |
| 809 | − || par->codec_id == AV_CODEC_ID_EAC3; | |
| 821 | + extended_flv = (par->codec_id == AV_CODEC_ID_AAC && track_idx) | |
| 822 | + || (par->codec_id == AV_CODEC_ID_MP3 && track_idx) | |
| 823 | + || par->codec_id == AV_CODEC_ID_OPUS | |
| 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 | 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 | 836 | write_codec_fourcc(pb, par->codec_id); |
| 814 | 837 | |
| 838 | + if (track_idx) | |
| 839 | + avio_w8(pb, track_idx); | |
| 840 | + | |
| 815 | 841 | if (par->codec_id == AV_CODEC_ID_AAC) { |
| 816 | 842 | flv_write_aac_header(s, par); |
| 817 | 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 | 851 | flv_write_aac_header(s, par); |
| 826 | 852 | } |
| 827 | 853 | } else { |
| 828 | − int track_idx = flv->video_track_idx_map[stream_index]; | |
| 829 | 854 | // If video stream has track_idx > 0 we need to send H.264 as extended video packet |
| 830 | 855 | extended_flv = (par->codec_id == AV_CODEC_ID_H264 && track_idx) || |
| 831 | 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 | 894 | if (par->codec_type == AVMEDIA_TYPE_AUDIO && (extended_flv || |
| 870 | 895 | (av_channel_layout_compare(&par->ch_layout, &(AVChannelLayout)AV_CHANNEL_LAYOUT_STEREO) == 1 && |
| 871 | 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 | 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 | 956 | static int flv_init(struct AVFormatContext *s) |
| 932 | 957 | { |
| 933 | 958 | int i; |
| 934 | − int video_ctr = 0; | |
| 959 | + int video_ctr = 0, audio_ctr = 0; | |
| 935 | 960 | FLVContext *flv = s->priv_data; |
| 936 | 961 | |
| 937 | 962 | flv->last_ts = av_calloc(s->nb_streams, sizeof(*flv->last_ts)); |
| 938 | 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)); | |
| 940 | − if (!flv->last_ts || !flv->metadata_pkt_written || !flv->video_track_idx_map) | |
| 964 | + flv->track_idx_map = av_calloc(s->nb_streams, sizeof(*flv->track_idx_map)); | |
| 965 | + if (!flv->last_ts || !flv->metadata_pkt_written || !flv->track_idx_map) | |
| 941 | 966 | return AVERROR(ENOMEM); |
| 942 | 967 | |
| 943 | 968 | for (i = 0; i < s->nb_streams; i++) { |
| @@ -949,7 +974,7 @@ static int flv_init(struct AVFormatContext *s) · 5 unmodified lines | ||
| 949 | 974 | s->streams[i]->avg_frame_rate.num) { |
| 950 | 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 | 978 | if (flv->video_par && flv->flags & FLV_ADD_KEYFRAME_INDEX) { |
| 954 | 979 | av_log(s, AV_LOG_ERROR, |
| 955 | 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 | 1006 | break; |
| 982 | 1007 | case AVMEDIA_TYPE_AUDIO: |
| 983 | − if (flv->audio_par) { | |
| 984 | − av_log(s, AV_LOG_ERROR, | |
| 985 | − "at most one audio stream is supported in flv\n"); | |
| 1008 | + if (audio_ctr && | |
| 1009 | + par->codec_id != AV_CODEC_ID_AAC && | |
| 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 | 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 | 1024 | if (get_audio_flags(s, par) < 0) |
| 990 | 1025 | return unsupported_codec(s, "Audio", par->codec_id); |
| 991 | 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 | 1192 | uint8_t frametype = pkt->flags & AV_PKT_FLAG_KEY ? FLV_FRAME_KEY : FLV_FRAME_INTER; |
| 1158 | 1193 | int flags = -1, flags_size, ret = 0; |
| 1159 | 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 | 1200 | || par->codec_id == AV_CODEC_ID_FLAC |
| 1164 | 1201 | || par->codec_id == AV_CODEC_ID_AC3 |
| 1165 | 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 | 1213 | else |
| 1177 | 1214 | flags_size = 1; |
| 1178 | 1215 | |
| 1179 | − if (par->codec_type == AVMEDIA_TYPE_VIDEO && track_idx) | |
| 1180 | − flags_size += 2; // additional header bytes for multi-track video | |
| 1216 | + if ((par->codec_type == AVMEDIA_TYPE_VIDEO || par->codec_type == AVMEDIA_TYPE_AUDIO) && track_idx) | |
| 1217 | + flags_size += 2; // additional header bytes for multi-track flv | |
| 1181 | 1218 | |
| 1182 | 1219 | if ((par->codec_id == AV_CODEC_ID_HEVC || |
| 1183 | 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 | 1381 | if (h2645 && pkttype == PacketTypeCodedFrames) |
| 1345 | 1382 | avio_wb24(pb, pkt->pts - pkt->dts); |
| 1346 | 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 | 1390 | write_codec_fourcc(pb, par->codec_id); |
| 1391 | + if (track_idx) | |
| 1392 | + avio_w8(pb, track_idx); | |
| 1349 | 1393 | } else { |
| 1350 | 1394 | av_assert1(flags >= 0); |
| 1351 | 1395 | avio_w8(pb, flags); |
| @@ -1435,7 +1479,7 @@ static void flv_deinit(AVFormatContext *s) · 83 unmodified lines | ||
| 1435 | 1479 | |
| 1436 | 1480 | av_freep(&flv->last_ts); |
| 1437 | 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 | 1485 | static const AVOption options[] = { |