Browse code

Get audio_service_type for AC-3 based on bitstream mode in the AC-3 parser and decoder, and vice-versa for the AC-3 encoder.

Justin Ruggles authored on 2011/03/25 01:10:38
Showing 8 changed files
... ...
@@ -94,6 +94,7 @@ get_next:
94 94
             avctx->channel_layout = s->channel_layout;
95 95
         }
96 96
         avctx->frame_size = s->samples;
97
+        avctx->audio_service_type = s->service_type;
97 98
     }
98 99
 
99 100
     avctx->bit_rate = s->bit_rate;
... ...
@@ -49,6 +49,7 @@ typedef struct AACAC3ParseContext {
49 49
     int bit_rate;
50 50
     int samples;
51 51
     int64_t channel_layout;
52
+    int service_type;
52 53
 
53 54
     int remaining_size;
54 55
     uint64_t state;
... ...
@@ -87,6 +87,7 @@ typedef struct {
87 87
     uint16_t crc1;
88 88
     uint8_t sr_code;
89 89
     uint8_t bitstream_id;
90
+    uint8_t bitstream_mode;
90 91
     uint8_t channel_mode;
91 92
     uint8_t lfe_on;
92 93
     uint8_t frame_type;
... ...
@@ -69,7 +69,7 @@ int ff_ac3_parse_header(GetBitContext *gbc, AC3HeaderInfo *hdr)
69 69
 
70 70
         skip_bits(gbc, 5); // skip bsid, already got it
71 71
 
72
-        skip_bits(gbc, 3); // skip bitstream mode
72
+        hdr->bitstream_mode = get_bits(gbc, 3);
73 73
         hdr->channel_mode = get_bits(gbc, 3);
74 74
 
75 75
         if(hdr->channel_mode == AC3_CHMODE_STEREO) {
... ...
@@ -151,6 +151,9 @@ static int ac3_sync(uint64_t state, AACAC3ParseContext *hdr_info,
151 151
     hdr_info->channels = hdr.channels;
152 152
     hdr_info->channel_layout = hdr.channel_layout;
153 153
     hdr_info->samples = hdr.num_blocks * 256;
154
+    hdr_info->service_type = hdr.bitstream_mode;
155
+    if (hdr.bitstream_mode == 0x7 && hdr.channels > 1)
156
+        hdr_info->service_type = AV_AUDIO_SERVICE_TYPE_KARAOKE;
154 157
     if(hdr.bitstream_id>10)
155 158
         hdr_info->codec_id = CODEC_ID_EAC3;
156 159
     else if (hdr_info->codec_id == CODEC_ID_NONE)
... ...
@@ -273,6 +273,7 @@ static int parse_frame_header(AC3DecodeContext *s)
273 273
 
274 274
     /* get decoding parameters from header info */
275 275
     s->bit_alloc_params.sr_code     = hdr.sr_code;
276
+    s->bitstream_mode               = hdr.bitstream_mode;
276 277
     s->channel_mode                 = hdr.channel_mode;
277 278
     s->channel_layout               = hdr.channel_layout;
278 279
     s->lfe_on                       = hdr.lfe_on;
... ...
@@ -1399,6 +1400,10 @@ static int ac3_decode_frame(AVCodecContext * avctx, void *data, int *data_size,
1399 1399
         if(s->out_channels < s->channels)
1400 1400
             s->output_mode  = s->out_channels == 1 ? AC3_CHMODE_MONO : AC3_CHMODE_STEREO;
1401 1401
     }
1402
+    /* set audio service type based on bitstream mode for AC-3 */
1403
+    avctx->audio_service_type = s->bitstream_mode;
1404
+    if (s->bitstream_mode == 0x7 && s->channels > 1)
1405
+        avctx->audio_service_type = AV_AUDIO_SERVICE_TYPE_KARAOKE;
1402 1406
 
1403 1407
     /* decode the audio blocks */
1404 1408
     channel_map = ff_ac3_dec_channel_map[s->output_mode & ~AC3_OUTPUT_LFEON][s->lfe_on];
... ...
@@ -79,6 +79,7 @@ typedef struct {
79 79
     int bit_rate;                           ///< stream bit rate, in bits-per-second
80 80
     int sample_rate;                        ///< sample frequency, in Hz
81 81
     int num_blocks;                         ///< number of audio blocks
82
+    int bitstream_mode;                     ///< bitstream mode                         (bsmod)
82 83
     int channel_mode;                       ///< channel mode                           (acmod)
83 84
     int channel_layout;                     ///< channel layout
84 85
     int lfe_on;                             ///< lfe channel in use
... ...
@@ -1657,6 +1657,18 @@ static av_cold int validate_options(AVCodecContext *avctx, AC3EncodeContext *s)
1657 1657
     if (s->cutoff > (s->sample_rate >> 1))
1658 1658
         s->cutoff = s->sample_rate >> 1;
1659 1659
 
1660
+    /* validate audio service type / channels combination */
1661
+    if ((avctx->audio_service_type == AV_AUDIO_SERVICE_TYPE_KARAOKE &&
1662
+         avctx->channels == 1) ||
1663
+        ((avctx->audio_service_type == AV_AUDIO_SERVICE_TYPE_COMMENTARY ||
1664
+          avctx->audio_service_type == AV_AUDIO_SERVICE_TYPE_EMERGENCY  ||
1665
+          avctx->audio_service_type == AV_AUDIO_SERVICE_TYPE_VOICE_OVER)
1666
+         && avctx->channels > 1)) {
1667
+        av_log(avctx, AV_LOG_ERROR, "invalid audio service type for the "
1668
+                                    "specified number of channels\n");
1669
+        return AVERROR(EINVAL);
1670
+    }
1671
+
1660 1672
     return 0;
1661 1673
 }
1662 1674
 
... ...
@@ -1799,7 +1811,9 @@ static av_cold int ac3_encode_init(AVCodecContext *avctx)
1799 1799
         return ret;
1800 1800
 
1801 1801
     s->bitstream_id   = 8 + s->bit_alloc.sr_shift;
1802
-    s->bitstream_mode = 0; /* complete main audio service */
1802
+    s->bitstream_mode = avctx->audio_service_type;
1803
+    if (s->bitstream_mode == AV_AUDIO_SERVICE_TYPE_KARAOKE)
1804
+        s->bitstream_mode = 0x7;
1803 1805
 
1804 1806
     s->frame_size_min  = 2 * ff_ac3_frame_size_tab[s->frame_size_code][s->bit_alloc.sr_code];
1805 1807
     s->bits_written    = 0;
... ...
@@ -410,7 +410,7 @@ int ff_eac3_parse_header(AC3DecodeContext *s)
410 410
 
411 411
     /* informational metadata */
412 412
     if (get_bits1(gbc)) {
413
-        skip_bits(gbc, 3); // skip bit stream mode
413
+        s->bitstream_mode = get_bits(gbc, 3);
414 414
         skip_bits(gbc, 2); // skip copyright bit and original bitstream bit
415 415
         if (s->channel_mode == AC3_CHMODE_STEREO) {
416 416
             skip_bits(gbc, 4); // skip Dolby surround and headphone mode