]> git.sesse.net Git - ffmpeg/blob - libavformat/ac3dec.c
Merge remote-tracking branch 'qatar/master'
[ffmpeg] / libavformat / ac3dec.c
1 /*
2  * RAW AC-3 and E-AC-3 demuxer
3  * Copyright (c) 2007 Justin Ruggles <justin.ruggles@gmail.com>
4  *
5  * This file is part of FFmpeg.
6  *
7  * FFmpeg is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU Lesser General Public
9  * License as published by the Free Software Foundation; either
10  * version 2.1 of the License, or (at your option) any later version.
11  *
12  * FFmpeg is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15  * Lesser General Public License for more details.
16  *
17  * You should have received a copy of the GNU Lesser General Public
18  * License along with FFmpeg; if not, write to the Free Software
19  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20  */
21
22 #include "libavutil/crc.h"
23 #include "libavcodec/ac3_parser.h"
24 #include "avformat.h"
25 #include "rawdec.h"
26
27 static int ac3_eac3_probe(AVProbeData *p, enum AVCodecID expected_codec_id)
28 {
29     int max_frames, first_frames = 0, frames;
30     uint8_t *buf, *buf2, *end;
31     AC3HeaderInfo hdr;
32     GetBitContext gbc;
33     enum AVCodecID codec_id = AV_CODEC_ID_AC3;
34
35     max_frames = 0;
36     buf = p->buf;
37     end = buf + p->buf_size;
38
39     for(; buf < end; buf++) {
40         if(buf > p->buf && (buf[0] != 0x0B || buf[1] != 0x77) )
41             continue;
42         buf2 = buf;
43
44         for(frames = 0; buf2 < end; frames++) {
45             if(!memcmp(buf2, "\x1\x10\0\0\0\0\0\0", 8))
46                 buf2+=16;
47             init_get_bits(&gbc, buf2, 54);
48             if(avpriv_ac3_parse_header(&gbc, &hdr) < 0)
49                 break;
50             if(buf2 + hdr.frame_size > end ||
51                av_crc(av_crc_get_table(AV_CRC_16_ANSI), 0, buf2 + 2, hdr.frame_size - 2))
52                 break;
53             if (hdr.bitstream_id > 10)
54                 codec_id = AV_CODEC_ID_EAC3;
55             buf2 += hdr.frame_size;
56         }
57         max_frames = FFMAX(max_frames, frames);
58         if(buf == p->buf)
59             first_frames = frames;
60     }
61     if(codec_id != expected_codec_id) return 0;
62     // keep this in sync with mp3 probe, both need to avoid
63     // issues with MPEG-files!
64     if   (first_frames>=4) return AVPROBE_SCORE_MAX/2+1;
65     else if(max_frames>200)return AVPROBE_SCORE_MAX/2;
66     else if(max_frames>=4) return AVPROBE_SCORE_MAX/4;
67     else if(max_frames>=1) return 1;
68     else                   return 0;
69 }
70
71 #if CONFIG_AC3_DEMUXER
72 static int ac3_probe(AVProbeData *p)
73 {
74     return ac3_eac3_probe(p, AV_CODEC_ID_AC3);
75 }
76
77 AVInputFormat ff_ac3_demuxer = {
78     .name           = "ac3",
79     .long_name      = NULL_IF_CONFIG_SMALL("raw AC-3"),
80     .read_probe     = ac3_probe,
81     .read_header    = ff_raw_audio_read_header,
82     .read_packet    = ff_raw_read_partial_packet,
83     .flags= AVFMT_GENERIC_INDEX,
84     .extensions = "ac3",
85     .raw_codec_id   = AV_CODEC_ID_AC3,
86 };
87 #endif
88
89 #if CONFIG_EAC3_DEMUXER
90 static int eac3_probe(AVProbeData *p)
91 {
92     return ac3_eac3_probe(p, AV_CODEC_ID_EAC3);
93 }
94
95 AVInputFormat ff_eac3_demuxer = {
96     .name           = "eac3",
97     .long_name      = NULL_IF_CONFIG_SMALL("raw E-AC-3"),
98     .read_probe     = eac3_probe,
99     .read_header    = ff_raw_audio_read_header,
100     .read_packet    = ff_raw_read_partial_packet,
101     .flags          = AVFMT_GENERIC_INDEX,
102     .extensions     = "eac3",
103     .raw_codec_id   = AV_CODEC_ID_EAC3,
104 };
105 #endif