ac3dec.c
Go to the documentation of this file.
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 Libav.
6  *
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.
11  *
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.
16  *
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
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 CodecID expected_codec_id)
28 {
29  int max_frames, first_frames = 0, frames;
30  uint8_t *buf, *buf2, *end;
31  AC3HeaderInfo hdr;
32  GetBitContext gbc;
34 
35  max_frames = 0;
36  buf = p->buf;
37  end = buf + p->buf_size;
38 
39  for(; buf < end; buf++) {
40  buf2 = buf;
41 
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 = CODEC_ID_EAC3;
51  buf2 += hdr.frame_size;
52  }
53  max_frames = FFMAX(max_frames, frames);
54  if(buf == p->buf)
55  first_frames = frames;
56  }
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;
61  else if(max_frames>500)return AVPROBE_SCORE_MAX/2;
62  else if(max_frames>=4) return AVPROBE_SCORE_MAX/4;
63  else if(max_frames>=1) return 1;
64  else return 0;
65 }
66 
67 #if CONFIG_AC3_DEMUXER
68 static int ac3_probe(AVProbeData *p)
69 {
70  return ac3_eac3_probe(p, CODEC_ID_AC3);
71 }
72 
73 AVInputFormat ff_ac3_demuxer = {
74  .name = "ac3",
75  .long_name = NULL_IF_CONFIG_SMALL("raw AC-3"),
76  .read_probe = ac3_probe,
77  .read_header = ff_raw_audio_read_header,
78  .read_packet = ff_raw_read_partial_packet,
79  .flags= AVFMT_GENERIC_INDEX,
80  .extensions = "ac3",
81  .value = CODEC_ID_AC3,
82 };
83 #endif
84 
85 #if CONFIG_EAC3_DEMUXER
86 static int eac3_probe(AVProbeData *p)
87 {
88  return ac3_eac3_probe(p, CODEC_ID_EAC3);
89 }
90 
91 AVInputFormat ff_eac3_demuxer = {
92  .name = "eac3",
93  .long_name = NULL_IF_CONFIG_SMALL("raw E-AC-3"),
94  .read_probe = eac3_probe,
95  .read_header = ff_raw_audio_read_header,
96  .read_packet = ff_raw_read_partial_packet,
97  .flags= AVFMT_GENERIC_INDEX,
98  .extensions = "eac3",
99  .value = CODEC_ID_EAC3,
100 };
101 #endif
uint32_t av_crc(const AVCRC *ctx, uint32_t crc, const uint8_t *buffer, size_t length)
Calculate the CRC of a block.
Definition: crc.c:118
const AVCRC * av_crc_get_table(AVCRCId crc_id)
Get an initialized standard CRC table.
Definition: crc.c:97
uint8_t bitstream_id
Definition: ac3.h:101
enum CodecID codec_id
Definition: mov_chan.c:417
int ff_raw_read_partial_packet(AVFormatContext *s, AVPacket *pkt)
Definition: rawdec.c:103
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification. ...
Definition: internal.h:191
Coded AC-3 header values up to the lfeon element, plus derived values.
Definition: ac3.h:94
#define FFMAX(a, b)
Definition: common.h:53
int buf_size
Size of buf except extra allocated bytes.
Definition: avformat.h:342
unsigned char * buf
Buffer must have AVPROBE_PADDING_SIZE of extra allocated bytes filled with zero.
Definition: avformat.h:341
static int max_frames[4]
Definition: ffmpeg.c:136
static int ac3_eac3_probe(AVProbeData *p, enum CodecID expected_codec_id)
Definition: ac3dec.c:27
int avpriv_ac3_parse_header(GetBitContext *gbc, AC3HeaderInfo *hdr)
Parse AC-3 frame header.
Definition: ac3_parser.c:50
#define AVFMT_GENERIC_INDEX
Use generic index building code.
Definition: avformat.h:376
static int init_get_bits(GetBitContext *s, const uint8_t *buffer, int bit_size)
Initialize GetBitContext.
Definition: get_bits.h:354
uint16_t frame_size
Definition: ac3.h:120
This structure contains the data a format has to probe a file.
Definition: avformat.h:339
int ff_raw_audio_read_header(AVFormatContext *s, AVFormatParameters *ap)
Definition: rawdec.c:123
#define AVPROBE_SCORE_MAX
maximum score, half of that is used for file-extension-based detection
Definition: avformat.h:345
Main libavformat public API header.
CodecID
Identify the syntax and semantics of the bitstream.
Definition: avcodec.h:83
const char * name
A comma separated list of short names for the format.
Definition: avformat.h:460