From b828c3954e61145957b60653a1e0957072221923 Mon Sep 17 00:00:00 2001 From: Vittorio Giovara Date: Fri, 31 Mar 2017 17:14:42 +0200 Subject: [PATCH] mov: convert to new channel layout API Signed-off-by: Vittorio Giovara Signed-off-by: Anton Khirnov Signed-off-by: James Almer --- libavformat/isom.c | 5 +-- libavformat/mov.c | 80 +++++++++++++++++++++++++++--------------- libavformat/mov_chan.c | 23 ++++++++---- libavformat/mov_chan.h | 3 +- libavformat/movenc.c | 25 +++++++------ 5 files changed, 87 insertions(+), 49 deletions(-) diff --git a/libavformat/isom.c b/libavformat/isom.c index 015c82e1bb..e6569dfb68 100644 --- a/libavformat/isom.c +++ b/libavformat/isom.c @@ -360,7 +360,8 @@ int ff_mp4_read_dec_config_descr(AVFormatContext *fc, AVStream *st, AVIOContext st->codecpar->extradata_size, 1, fc); if (ret < 0) return ret; - st->codecpar->channels = cfg.channels; + st->codecpar->ch_layout.order = AV_CHANNEL_ORDER_UNSPEC; + st->codecpar->ch_layout.nb_channels = cfg.channels; if (cfg.object_type == 29 && cfg.sampling_index < 3) // old mp3on4 st->codecpar->sample_rate = ff_mpa_freq_tab[cfg.sampling_index]; else if (cfg.ext_sample_rate) @@ -368,7 +369,7 @@ int ff_mp4_read_dec_config_descr(AVFormatContext *fc, AVStream *st, AVIOContext else st->codecpar->sample_rate = cfg.sample_rate; av_log(fc, AV_LOG_TRACE, "mp4a config channels %d obj %d ext obj %d " - "sample rate %d ext sample rate %d\n", st->codecpar->channels, + "sample rate %d ext sample rate %d\n", cfg.channels, cfg.object_type, cfg.ext_object_type, cfg.sample_rate, cfg.ext_sample_rate); if (!(st->codecpar->codec_id = ff_codec_get_id(mp4_audio_types, diff --git a/libavformat/mov.c b/libavformat/mov.c index a842bf4464..62744a1961 100644 --- a/libavformat/mov.c +++ b/libavformat/mov.c @@ -799,6 +799,7 @@ static int mov_read_dac3(MOVContext *c, AVIOContext *pb, MOVAtom atom) AVStream *st; enum AVAudioServiceType *ast; int ac3info, acmod, lfeon, bsmod; + uint64_t mask; if (c->fc->nb_streams < 1) return 0; @@ -813,12 +814,15 @@ static int mov_read_dac3(MOVContext *c, AVIOContext *pb, MOVAtom atom) bsmod = (ac3info >> 14) & 0x7; acmod = (ac3info >> 11) & 0x7; lfeon = (ac3info >> 10) & 0x1; - st->codecpar->channels = ((int[]){2,1,2,3,3,4,4,5})[acmod] + lfeon; - st->codecpar->channel_layout = ff_ac3_channel_layout_tab[acmod]; + + mask = ff_ac3_channel_layout_tab[acmod]; if (lfeon) - st->codecpar->channel_layout |= AV_CH_LOW_FREQUENCY; + mask |= AV_CH_LOW_FREQUENCY; + av_channel_layout_uninit(&st->codecpar->ch_layout); + av_channel_layout_from_mask(&st->codecpar->ch_layout, mask); + *ast = bsmod; - if (st->codecpar->channels > 1 && bsmod == 0x7) + if (st->codecpar->ch_layout.nb_channels > 1 && bsmod == 0x7) *ast = AV_AUDIO_SERVICE_TYPE_KARAOKE; return 0; @@ -829,6 +833,7 @@ static int mov_read_dec3(MOVContext *c, AVIOContext *pb, MOVAtom atom) AVStream *st; enum AVAudioServiceType *ast; int eac3info, acmod, lfeon, bsmod; + uint64_t mask; if (c->fc->nb_streams < 1) return 0; @@ -847,12 +852,15 @@ static int mov_read_dec3(MOVContext *c, AVIOContext *pb, MOVAtom atom) bsmod = (eac3info >> 12) & 0x1f; acmod = (eac3info >> 9) & 0x7; lfeon = (eac3info >> 8) & 0x1; - st->codecpar->channel_layout = ff_ac3_channel_layout_tab[acmod]; + + mask = ff_ac3_channel_layout_tab[acmod]; if (lfeon) - st->codecpar->channel_layout |= AV_CH_LOW_FREQUENCY; - st->codecpar->channels = av_get_channel_layout_nb_channels(st->codecpar->channel_layout); + mask |= AV_CH_LOW_FREQUENCY; + av_channel_layout_uninit(&st->codecpar->ch_layout); + av_channel_layout_from_mask(&st->codecpar->ch_layout, mask); + *ast = bsmod; - if (st->codecpar->channels > 1 && bsmod == 0x7) + if (st->codecpar->ch_layout.nb_channels > 1 && bsmod == 0x7) *ast = AV_AUDIO_SERVICE_TYPE_KARAOKE; return 0; @@ -899,15 +907,14 @@ static int mov_read_ddts(MOVContext *c, AVIOContext *pb, MOVAtom atom) if (channel_layout_code > 0xff) { av_log(c->fc, AV_LOG_WARNING, "Unsupported DTS audio channel layout\n"); } - st->codecpar->channel_layout = + av_channel_layout_uninit(&st->codecpar->ch_layout); + av_channel_layout_from_mask(&st->codecpar->ch_layout, ((channel_layout_code & 0x1) ? AV_CH_FRONT_CENTER : 0) | ((channel_layout_code & 0x2) ? AV_CH_FRONT_LEFT : 0) | ((channel_layout_code & 0x2) ? AV_CH_FRONT_RIGHT : 0) | ((channel_layout_code & 0x4) ? AV_CH_SIDE_LEFT : 0) | ((channel_layout_code & 0x4) ? AV_CH_SIDE_RIGHT : 0) | - ((channel_layout_code & 0x8) ? AV_CH_LOW_FREQUENCY : 0); - - st->codecpar->channels = av_get_channel_layout_nb_channels(st->codecpar->channel_layout); + ((channel_layout_code & 0x8) ? AV_CH_LOW_FREQUENCY : 0)); return 0; } @@ -2163,14 +2170,18 @@ static void mov_parse_stsd_audio(MOVContext *c, AVIOContext *pb, uint16_t version = avio_rb16(pb); uint32_t id = 0; AVDictionaryEntry *compatible_brands = av_dict_get(c->fc->metadata, "compatible_brands", NULL, AV_DICT_MATCH_CASE); + int channel_count; avio_rb16(pb); /* revision level */ id = avio_rl32(pb); /* vendor */ av_dict_set(&st->metadata, "vendor_id", av_fourcc2str(id), 0); - st->codecpar->channels = avio_rb16(pb); /* channel count */ + channel_count = avio_rb16(pb); + + st->codecpar->ch_layout.order = AV_CHANNEL_ORDER_UNSPEC; + st->codecpar->ch_layout.nb_channels = channel_count; st->codecpar->bits_per_coded_sample = avio_rb16(pb); /* sample size */ - av_log(c->fc, AV_LOG_TRACE, "audio channels %d\n", st->codecpar->channels); + av_log(c->fc, AV_LOG_TRACE, "audio channels %d\n", channel_count); sc->audio_cid = avio_rb16(pb); avio_rb16(pb); /* packet size = 0 */ @@ -2190,7 +2201,9 @@ static void mov_parse_stsd_audio(MOVContext *c, AVIOContext *pb, } else if (version == 2) { avio_rb32(pb); /* sizeof struct only */ st->codecpar->sample_rate = av_int2double(avio_rb64(pb)); - st->codecpar->channels = avio_rb32(pb); + channel_count = avio_rb32(pb); + st->codecpar->ch_layout.order = AV_CHANNEL_ORDER_UNSPEC; + st->codecpar->ch_layout.nb_channels = channel_count; avio_rb32(pb); /* always 0x7F000000 */ st->codecpar->bits_per_coded_sample = avio_rb32(pb); @@ -2242,15 +2255,15 @@ static void mov_parse_stsd_audio(MOVContext *c, AVIOContext *pb, /* set values for old format before stsd version 1 appeared */ case AV_CODEC_ID_MACE3: sc->samples_per_frame = 6; - sc->bytes_per_frame = 2 * st->codecpar->channels; + sc->bytes_per_frame = 2 * st->codecpar->ch_layout.nb_channels; break; case AV_CODEC_ID_MACE6: sc->samples_per_frame = 6; - sc->bytes_per_frame = 1 * st->codecpar->channels; + sc->bytes_per_frame = 1 * st->codecpar->ch_layout.nb_channels; break; case AV_CODEC_ID_ADPCM_IMA_QT: sc->samples_per_frame = 64; - sc->bytes_per_frame = 34 * st->codecpar->channels; + sc->bytes_per_frame = 34 * st->codecpar->ch_layout.nb_channels; break; case AV_CODEC_ID_GSM: sc->samples_per_frame = 160; @@ -2261,9 +2274,9 @@ static void mov_parse_stsd_audio(MOVContext *c, AVIOContext *pb, } bits_per_sample = av_get_bits_per_sample(st->codecpar->codec_id); - if (bits_per_sample && (bits_per_sample >> 3) * (uint64_t)st->codecpar->channels <= INT_MAX) { + if (bits_per_sample && (bits_per_sample >> 3) * (uint64_t)st->codecpar->ch_layout.nb_channels <= INT_MAX) { st->codecpar->bits_per_coded_sample = bits_per_sample; - sc->sample_size = (bits_per_sample >> 3) * st->codecpar->channels; + sc->sample_size = (bits_per_sample >> 3) * st->codecpar->ch_layout.nb_channels; } } @@ -2403,7 +2416,8 @@ static int mov_finalize_stsd_codec(MOVContext *c, AVIOContext *pb, #endif /* no ifdef since parameters are always those */ case AV_CODEC_ID_QCELP: - st->codecpar->channels = 1; + av_channel_layout_uninit(&st->codecpar->ch_layout); + st->codecpar->ch_layout = (AVChannelLayout)AV_CHANNEL_LAYOUT_MONO; // force sample rate for qcelp when not stored in mov if (st->codecpar->codec_tag != MKTAG('Q','c','l','p')) st->codecpar->sample_rate = 8000; @@ -2413,12 +2427,14 @@ static int mov_finalize_stsd_codec(MOVContext *c, AVIOContext *pb, sc->bytes_per_frame = 35; break; case AV_CODEC_ID_AMR_NB: - st->codecpar->channels = 1; + av_channel_layout_uninit(&st->codecpar->ch_layout); + st->codecpar->ch_layout = (AVChannelLayout)AV_CHANNEL_LAYOUT_MONO; /* force sample rate for amr, stsd in 3gp does not store sample rate */ st->codecpar->sample_rate = 8000; break; case AV_CODEC_ID_AMR_WB: - st->codecpar->channels = 1; + av_channel_layout_uninit(&st->codecpar->ch_layout); + st->codecpar->ch_layout = (AVChannelLayout)AV_CHANNEL_LAYOUT_MONO; st->codecpar->sample_rate = 16000; break; case AV_CODEC_ID_MP2: @@ -2437,7 +2453,12 @@ static int mov_finalize_stsd_codec(MOVContext *c, AVIOContext *pb, break; case AV_CODEC_ID_ALAC: if (st->codecpar->extradata_size == 36) { - st->codecpar->channels = AV_RB8 (st->codecpar->extradata + 21); + int channel_count = AV_RB8(st->codecpar->extradata + 21); + if (st->codecpar->ch_layout.nb_channels != channel_count) { + av_channel_layout_uninit(&st->codecpar->ch_layout); + st->codecpar->ch_layout.order = AV_CHANNEL_ORDER_UNSPEC; + st->codecpar->ch_layout.nb_channels = channel_count; + } st->codecpar->sample_rate = AV_RB32(st->codecpar->extradata + 32); } break; @@ -2544,8 +2565,8 @@ int ff_mov_read_stsd_entries(MOVContext *c, AVIOContext *pb, int entries) av_log(c->fc, AV_LOG_ERROR, "Invalid sample rate %d\n", st->codecpar->sample_rate); return AVERROR_INVALIDDATA; } - if (st->codecpar->channels < 0) { - av_log(c->fc, AV_LOG_ERROR, "Invalid channels %d\n", st->codecpar->channels); + if (st->codecpar->ch_layout.nb_channels < 0) { + av_log(c->fc, AV_LOG_ERROR, "Invalid channels %d\n", st->codecpar->ch_layout.nb_channels); return AVERROR_INVALIDDATA; } } else if (st->codecpar->codec_type==AVMEDIA_TYPE_SUBTITLE){ @@ -7175,6 +7196,7 @@ static int mov_read_dmlp(MOVContext *c, AVIOContext *pb, MOVAtom atom) unsigned format_info; int channel_assignment, channel_assignment1, channel_assignment2; int ratebits; + uint64_t chmask; if (c->fc->nb_streams < 1) return 0; @@ -7195,8 +7217,10 @@ static int mov_read_dmlp(MOVContext *c, AVIOContext *pb, MOVAtom atom) st->codecpar->frame_size = 40 << (ratebits & 0x7); st->codecpar->sample_rate = mlp_samplerate(ratebits); - st->codecpar->channels = truehd_channels(channel_assignment); - st->codecpar->channel_layout = truehd_layout(channel_assignment); + + av_channel_layout_uninit(&st->codecpar->ch_layout); + chmask = truehd_layout(channel_assignment); + av_channel_layout_from_mask(&st->codecpar->ch_layout, chmask); return 0; } diff --git a/libavformat/mov_chan.c b/libavformat/mov_chan.c index 349634094c..f52239d347 100644 --- a/libavformat/mov_chan.c +++ b/libavformat/mov_chan.c @@ -499,7 +499,7 @@ static uint32_t mov_get_channel_label(uint32_t label) } uint32_t ff_mov_get_channel_layout_tag(enum AVCodecID codec_id, - uint64_t channel_layout, + const AVChannelLayout *ch_layout, uint32_t *bitmap) { int i, j; @@ -519,7 +519,7 @@ uint32_t ff_mov_get_channel_layout_tag(enum AVCodecID codec_id, const struct MovChannelLayoutMap *layout_map; /* get the layout map based on the channel count */ - channels = av_get_channel_layout_nb_channels(channel_layout); + channels = ch_layout->nb_channels; if (channels > 9) channels = 0; layout_map = mov_ch_layout_map[channels]; @@ -530,7 +530,8 @@ uint32_t ff_mov_get_channel_layout_tag(enum AVCodecID codec_id, continue; for (j = 0; layout_map[j].tag != 0; j++) { if (layout_map[j].tag == layouts[i] && - layout_map[j].layout == channel_layout) + (ch_layout->order == AV_CHANNEL_ORDER_NATIVE && + layout_map[j].layout == ch_layout->u.mask)) break; } if (layout_map[j].tag) @@ -540,9 +541,11 @@ uint32_t ff_mov_get_channel_layout_tag(enum AVCodecID codec_id, } /* if no tag was found, use channel bitmap as a backup if possible */ - if (tag == 0 && channel_layout > 0 && channel_layout < 0x40000) { + if (tag == 0 && av_channel_layout_check(ch_layout) && + ch_layout->order == AV_CHANNEL_ORDER_NATIVE && + ch_layout->u.mask < 0x40000) { tag = MOV_CH_LAYOUT_USE_BITMAP; - *bitmap = (uint32_t)channel_layout; + *bitmap = (uint32_t)ch_layout->u.mask; } else *bitmap = 0; @@ -555,6 +558,7 @@ int ff_mov_read_chan(AVFormatContext *s, AVIOContext *pb, AVStream *st, int64_t size) { uint32_t layout_tag, bitmap, num_descr, label_mask; + uint64_t mask = 0; int i; if (size < 12) @@ -596,9 +600,14 @@ int ff_mov_read_chan(AVFormatContext *s, AVIOContext *pb, AVStream *st, } if (layout_tag == 0) { if (label_mask) - st->codecpar->channel_layout = label_mask; + mask = label_mask; } else - st->codecpar->channel_layout = mov_get_channel_layout(layout_tag, bitmap); + mask = mov_get_channel_layout(layout_tag, bitmap); + + if (mask) { + av_channel_layout_uninit(&st->codecpar->ch_layout); + av_channel_layout_from_mask(&st->codecpar->ch_layout, mask); + } avio_skip(pb, size - 12); return 0; diff --git a/libavformat/mov_chan.h b/libavformat/mov_chan.h index f7916e9899..9514dfa405 100644 --- a/libavformat/mov_chan.h +++ b/libavformat/mov_chan.h @@ -28,6 +28,7 @@ #include +#include "libavutil/channel_layout.h" #include "libavcodec/codec_id.h" #include "avformat.h" @@ -41,7 +42,7 @@ * @return channel layout tag */ uint32_t ff_mov_get_channel_layout_tag(enum AVCodecID codec_id, - uint64_t channel_layout, + const AVChannelLayout *ch_layout, uint32_t *bitmap); /** diff --git a/libavformat/movenc.c b/libavformat/movenc.c index ee1629de46..6fc1afb730 100644 --- a/libavformat/movenc.c +++ b/libavformat/movenc.c @@ -868,7 +868,7 @@ static int mov_write_chan_tag(AVFormatContext *s, AVIOContext *pb, MOVTrack *tra int64_t pos = avio_tell(pb); layout_tag = ff_mov_get_channel_layout_tag(track->par->codec_id, - track->par->channel_layout, + &track->par->ch_layout, &bitmap); if (!layout_tag) { av_log(s, AV_LOG_WARNING, "not writing 'chan' tag due to " @@ -1137,7 +1137,7 @@ static int mov_write_audio_tag(AVFormatContext *s, AVIOContext *pb, MOVMuxContex int ret = 0; if (track->mode == MODE_MOV) { - if (track->timescale > UINT16_MAX || !track->par->channels) { + if (track->timescale > UINT16_MAX || !track->par->ch_layout.nb_channels) { if (mov_get_lpcm_flags(track->par->codec_id)) tag = AV_RL32("lpcm"); version = 2; @@ -1173,7 +1173,7 @@ static int mov_write_audio_tag(AVFormatContext *s, AVIOContext *pb, MOVMuxContex avio_wb32(pb, 0x00010000); avio_wb32(pb, 72); avio_wb64(pb, av_double2int(track->par->sample_rate)); - avio_wb32(pb, track->par->channels); + avio_wb32(pb, track->par->ch_layout.nb_channels); avio_wb32(pb, 0x7F000000); avio_wb32(pb, av_get_bits_per_sample(track->par->codec_id)); avio_wb32(pb, mov_get_lpcm_flags(track->par->codec_id)); @@ -1181,7 +1181,7 @@ static int mov_write_audio_tag(AVFormatContext *s, AVIOContext *pb, MOVMuxContex avio_wb32(pb, get_samples_per_packet(track)); } else { if (track->mode == MODE_MOV) { - avio_wb16(pb, track->par->channels); + avio_wb16(pb, track->par->ch_layout.nb_channels); if (track->par->codec_id == AV_CODEC_ID_PCM_U8 || track->par->codec_id == AV_CODEC_ID_PCM_S8) avio_wb16(pb, 8); /* bits per sample */ @@ -1194,7 +1194,7 @@ static int mov_write_audio_tag(AVFormatContext *s, AVIOContext *pb, MOVMuxContex if (track->par->codec_id == AV_CODEC_ID_FLAC || track->par->codec_id == AV_CODEC_ID_ALAC || track->par->codec_id == AV_CODEC_ID_OPUS) { - avio_wb16(pb, track->par->channels); + avio_wb16(pb, track->par->ch_layout.nb_channels); } else { avio_wb16(pb, 2); } @@ -1226,7 +1226,7 @@ static int mov_write_audio_tag(AVFormatContext *s, AVIOContext *pb, MOVMuxContex avio_wb32(pb, 1); /* must be 1 for uncompressed formats */ else avio_wb32(pb, track->par->frame_size); /* Samples per packet */ - avio_wb32(pb, track->sample_size / track->par->channels); /* Bytes per packet */ + avio_wb32(pb, track->sample_size / track->par->ch_layout.nb_channels); /* Bytes per packet */ avio_wb32(pb, track->sample_size); /* Bytes per frame */ avio_wb32(pb, 2); /* Bytes per sample */ } @@ -4392,7 +4392,7 @@ static int mov_write_isml_manifest(AVIOContext *pb, MOVMuxContext *mov, AVFormat track->par->extradata_size); param_write_int(pb, "AudioTag", ff_codec_get_tag(ff_codec_wav_tags, track->par->codec_id)); - param_write_int(pb, "Channels", track->par->channels); + param_write_int(pb, "Channels", track->par->ch_layout.nb_channels); param_write_int(pb, "SamplingRate", track->par->sample_rate); param_write_int(pb, "BitsPerSample", 16); param_write_int(pb, "PacketSize", track->par->block_align ? @@ -5150,7 +5150,7 @@ static int mov_write_uuidprof_tag(AVIOContext *pb, AVFormatContext *s) avio_wb32(pb, audio_kbitrate); avio_wb32(pb, audio_kbitrate); avio_wb32(pb, audio_rate); - avio_wb32(pb, audio_par->channels); + avio_wb32(pb, audio_par->ch_layout.nb_channels); avio_wb32(pb, 0x34); /* size */ ffio_wfourcc(pb, "VPRF"); /* video */ @@ -6803,7 +6803,8 @@ static int mov_init(AVFormatContext *s) }else if (st->codecpar->frame_size > 1){ /* assume compressed audio */ track->audio_vbr = 1; }else{ - track->sample_size = (av_get_bits_per_sample(st->codecpar->codec_id) >> 3) * st->codecpar->channels; + track->sample_size = (av_get_bits_per_sample(st->codecpar->codec_id) >> 3) * + st->codecpar->ch_layout.nb_channels; } if (st->codecpar->codec_id == AV_CODEC_ID_ILBC || st->codecpar->codec_id == AV_CODEC_ID_ADPCM_IMA_QT) { @@ -6939,7 +6940,8 @@ static int mov_write_header(AVFormatContext *s) } if (st->codecpar->codec_type != AVMEDIA_TYPE_AUDIO || - track->par->channel_layout != AV_CH_LAYOUT_MONO) + av_channel_layout_compare(&track->par->ch_layout, + &(AVChannelLayout)AV_CHANNEL_LAYOUT_MONO)) continue; for (j = 0; j < s->nb_streams; j++) { @@ -6949,7 +6951,8 @@ static int mov_write_header(AVFormatContext *s) continue; if (stj->codecpar->codec_type != AVMEDIA_TYPE_AUDIO || - trackj->par->channel_layout != AV_CH_LAYOUT_MONO || + av_channel_layout_compare(&trackj->par->ch_layout, + &(AVChannelLayout)AV_CHANNEL_LAYOUT_MONO) || trackj->language != track->language || trackj->tag != track->tag )