Merge remote-tracking branch 'libav/master'
[FFMpeg-mirror/mplayer-patches.git] / libavformat / ac3dec.c
blobd3f2ec297d9b43385e9f90defe27a6b56f3d0a87
1 /*
2 * RAW AC-3 and E-AC-3 demuxer
3 * Copyright (c) 2007 Justin Ruggles <justin.ruggles@gmail.com>
5 * This file is part of Libav.
7 * Libav 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.
12 * Libav 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.
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with Libav; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
22 #include "libavutil/crc.h"
23 #include "libavcodec/ac3_parser.h"
24 #include "avformat.h"
25 #include "rawdec.h"
27 static int ac3_eac3_probe(AVProbeData *p, enum AVCodecID expected_codec_id)
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;
35 max_frames = 0;
36 buf = p->buf;
37 end = buf + p->buf_size;
39 for(; buf < end; buf++) {
40 buf2 = buf;
42 for(frames = 0; buf2 < end; frames++) {
43 init_get_bits(&gbc, buf2, 54);
44 if(avpriv_ac3_parse_header(&gbc, &hdr) < 0)
45 break;
46 if(buf2 + hdr.frame_size > end ||
47 av_crc(av_crc_get_table(AV_CRC_16_ANSI), 0, buf2 + 2, hdr.frame_size - 2))
48 break;
49 if (hdr.bitstream_id > 10)
50 codec_id = AV_CODEC_ID_EAC3;
51 buf2 += hdr.frame_size;
53 max_frames = FFMAX(max_frames, frames);
54 if(buf == p->buf)
55 first_frames = frames;
57 if(codec_id != expected_codec_id) return 0;
58 // keep this in sync with mp3 probe, both need to avoid
59 // issues with MPEG-files!
60 if (first_frames >= 4) return AVPROBE_SCORE_MAX / 2 + 1;
62 if (max_frames) {
63 int pes = 0, i;
64 unsigned int code = -1;
66 #define VIDEO_ID 0x000001e0
67 #define AUDIO_ID 0x000001c0
68 /* do a search for mpegps headers to be able to properly bias
69 * towards mpegps if we detect this stream as both. */
70 for (i = 0; i<p->buf_size; i++) {
71 code = (code << 8) + p->buf[i];
72 if ((code & 0xffffff00) == 0x100) {
73 if ((code & 0x1f0) == VIDEO_ID) pes++;
74 else if((code & 0x1e0) == AUDIO_ID) pes++;
78 if (pes)
79 max_frames = (max_frames + pes - 1) / pes;
81 if (max_frames > 500) return AVPROBE_SCORE_MAX / 2;
82 else if (max_frames >= 4) return AVPROBE_SCORE_MAX / 4;
83 else if (max_frames >= 1) return 1;
84 else return 0;
87 #if CONFIG_AC3_DEMUXER
88 static int ac3_probe(AVProbeData *p)
90 return ac3_eac3_probe(p, AV_CODEC_ID_AC3);
93 AVInputFormat ff_ac3_demuxer = {
94 .name = "ac3",
95 .long_name = NULL_IF_CONFIG_SMALL("raw AC-3"),
96 .read_probe = ac3_probe,
97 .read_header = ff_raw_audio_read_header,
98 .read_packet = ff_raw_read_partial_packet,
99 .flags= AVFMT_GENERIC_INDEX,
100 .extensions = "ac3",
101 .raw_codec_id = AV_CODEC_ID_AC3,
103 #endif
105 #if CONFIG_EAC3_DEMUXER
106 static int eac3_probe(AVProbeData *p)
108 return ac3_eac3_probe(p, AV_CODEC_ID_EAC3);
111 AVInputFormat ff_eac3_demuxer = {
112 .name = "eac3",
113 .long_name = NULL_IF_CONFIG_SMALL("raw E-AC-3"),
114 .read_probe = eac3_probe,
115 .read_header = ff_raw_audio_read_header,
116 .read_packet = ff_raw_read_partial_packet,
117 .flags = AVFMT_GENERIC_INDEX,
118 .extensions = "eac3",
119 .raw_codec_id = AV_CODEC_ID_EAC3,
121 #endif