]> git.sesse.net Git - ffmpeg/blob - libavformat/ac3dec.c
Merge commit '5352802da81f2083e65d466612e639a4e6e5530e'
[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/avassert.h"
23 #include "libavutil/crc.h"
24 #include "libavcodec/ac3_parser.h"
25 #include "avformat.h"
26 #include "rawdec.h"
27
28 static int ac3_eac3_probe(AVProbeData *p, enum AVCodecID expected_codec_id)
29 {
30     int max_frames, first_frames = 0, frames;
31     const uint8_t *buf, *buf2, *end;
32     enum AVCodecID codec_id = AV_CODEC_ID_AC3;
33
34     max_frames = 0;
35     buf = p->buf;
36     end = buf + p->buf_size;
37
38     for(; buf < end; buf++) {
39         if(buf > p->buf && !(buf[0] == 0x0B && buf[1] == 0x77)
40                         && !(buf[0] == 0x77 && buf[1] == 0x0B) )
41             continue;
42         buf2 = buf;
43
44         for(frames = 0; buf2 < end; frames++) {
45             uint8_t buf3[4096];
46             uint8_t bitstream_id;
47             uint16_t frame_size;
48             int i, ret;
49
50             if(!memcmp(buf2, "\x1\x10\0\0\0\0\0\0", 8))
51                 buf2+=16;
52             if (buf[0] == 0x77 && buf[1] == 0x0B) {
53                 for(i=0; i<8; i+=2) {
54                     buf3[i  ] = buf2[i+1];
55                     buf3[i+1] = buf2[i  ];
56                 }
57                 ret = av_ac3_parse_header(buf3, 8, &bitstream_id,
58                                           &frame_size);
59             }else
60                 ret = av_ac3_parse_header(buf2, end - buf2, &bitstream_id,
61                                           &frame_size);
62             if (ret < 0)
63                 break;
64             if(buf2 + frame_size > end)
65                 break;
66             if (buf[0] == 0x77 && buf[1] == 0x0B) {
67                 av_assert0(frame_size <= sizeof(buf3));
68                 for(i = 8; i < frame_size; i += 2) {
69                     buf3[i  ] = buf2[i+1];
70                     buf3[i+1] = buf2[i  ];
71                 }
72                 if (av_crc(av_crc_get_table(AV_CRC_16_ANSI), 0, buf3 + 2, frame_size - 2))
73                     break;
74             } else {
75                 if (av_crc(av_crc_get_table(AV_CRC_16_ANSI), 0, buf2 + 2, frame_size - 2))
76                     break;
77             }
78             if (bitstream_id > 10)
79                 codec_id = AV_CODEC_ID_EAC3;
80             buf2 += frame_size;
81         }
82         max_frames = FFMAX(max_frames, frames);
83         if(buf == p->buf)
84             first_frames = frames;
85     }
86     if(codec_id != expected_codec_id) return 0;
87     // keep this in sync with mp3 probe, both need to avoid
88     // issues with MPEG-files!
89     if   (first_frames>=7) return AVPROBE_SCORE_EXTENSION + 1;
90     else if(max_frames>200)return AVPROBE_SCORE_EXTENSION;
91     else if(max_frames>=4) return AVPROBE_SCORE_EXTENSION/2;
92     else if(max_frames>=1) return 1;
93     else                   return 0;
94 }
95
96 #if CONFIG_AC3_DEMUXER
97 static int ac3_probe(AVProbeData *p)
98 {
99     return ac3_eac3_probe(p, AV_CODEC_ID_AC3);
100 }
101
102 AVInputFormat ff_ac3_demuxer = {
103     .name           = "ac3",
104     .long_name      = NULL_IF_CONFIG_SMALL("raw AC-3"),
105     .read_probe     = ac3_probe,
106     .read_header    = ff_raw_audio_read_header,
107     .read_packet    = ff_raw_read_partial_packet,
108     .flags= AVFMT_GENERIC_INDEX,
109     .extensions = "ac3",
110     .raw_codec_id   = AV_CODEC_ID_AC3,
111 };
112 #endif
113
114 #if CONFIG_EAC3_DEMUXER
115 static int eac3_probe(AVProbeData *p)
116 {
117     return ac3_eac3_probe(p, AV_CODEC_ID_EAC3);
118 }
119
120 AVInputFormat ff_eac3_demuxer = {
121     .name           = "eac3",
122     .long_name      = NULL_IF_CONFIG_SMALL("raw E-AC-3"),
123     .read_probe     = eac3_probe,
124     .read_header    = ff_raw_audio_read_header,
125     .read_packet    = ff_raw_read_partial_packet,
126     .flags          = AVFMT_GENERIC_INDEX,
127     .extensions     = "eac3",
128     .raw_codec_id   = AV_CODEC_ID_EAC3,
129 };
130 #endif