FFmpeg
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
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 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  const uint8_t *buf, *buf2, *end;
31  AC3HeaderInfo *phdr = NULL;
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  if(buf > p->buf && !(buf[0] == 0x0B && buf[1] == 0x77)
41  && !(buf[0] == 0x77 && buf[1] == 0x0B) )
42  continue;
43  buf2 = buf;
44 
45  for(frames = 0; buf2 < end; frames++) {
46  uint8_t buf3[4096];
47  int i;
48  if(!memcmp(buf2, "\x1\x10\0\0\0\0\0\0", 8))
49  buf2+=16;
50  if (buf[0] == 0x77 && buf[1] == 0x0B) {
51  for(i=0; i<8; i+=2) {
52  buf3[i ] = buf[i+1];
53  buf3[i+1] = buf[i ];
54  }
55  init_get_bits(&gbc, buf3, 54);
56  }else
57  init_get_bits(&gbc, buf2, 54);
58  if(avpriv_ac3_parse_header(&gbc, &phdr) < 0)
59  break;
60  if(buf2 + phdr->frame_size > end)
61  break;
62  if (buf[0] == 0x77 && buf[1] == 0x0B) {
63  av_assert0(phdr->frame_size <= sizeof(buf3));
64  for(i=8; i<phdr->frame_size; i+=2) {
65  buf3[i ] = buf[i+1];
66  buf3[i+1] = buf[i ];
67  }
68  }
69  if(av_crc(av_crc_get_table(AV_CRC_16_ANSI), 0, gbc.buffer + 2, phdr->frame_size - 2))
70  break;
71  if (phdr->bitstream_id > 10)
72  codec_id = AV_CODEC_ID_EAC3;
73  buf2 += phdr->frame_size;
74  }
75  max_frames = FFMAX(max_frames, frames);
76  if(buf == p->buf)
77  first_frames = frames;
78  }
79  av_freep(&phdr);
80  if(codec_id != expected_codec_id) return 0;
81  // keep this in sync with mp3 probe, both need to avoid
82  // issues with MPEG-files!
83  if (first_frames>=7) return AVPROBE_SCORE_EXTENSION + 1;
84  else if(max_frames>200)return AVPROBE_SCORE_EXTENSION;
85  else if(max_frames>=4) return AVPROBE_SCORE_EXTENSION/2;
86  else if(max_frames>=1) return 1;
87  else return 0;
88 }
89 
90 #if CONFIG_AC3_DEMUXER
91 static int ac3_probe(AVProbeData *p)
92 {
94 }
95 
96 AVInputFormat ff_ac3_demuxer = {
97  .name = "ac3",
98  .long_name = NULL_IF_CONFIG_SMALL("raw AC-3"),
99  .read_probe = ac3_probe,
100  .read_header = ff_raw_audio_read_header,
101  .read_packet = ff_raw_read_partial_packet,
102  .flags= AVFMT_GENERIC_INDEX,
103  .extensions = "ac3",
104  .raw_codec_id = AV_CODEC_ID_AC3,
105 };
106 #endif
107 
108 #if CONFIG_EAC3_DEMUXER
109 static int eac3_probe(AVProbeData *p)
110 {
111  return ac3_eac3_probe(p, AV_CODEC_ID_EAC3);
112 }
113 
114 AVInputFormat ff_eac3_demuxer = {
115  .name = "eac3",
116  .long_name = NULL_IF_CONFIG_SMALL("raw E-AC-3"),
117  .read_probe = eac3_probe,
118  .read_header = ff_raw_audio_read_header,
119  .read_packet = ff_raw_read_partial_packet,
120  .flags = AVFMT_GENERIC_INDEX,
121  .extensions = "eac3",
122  .raw_codec_id = AV_CODEC_ID_EAC3,
123 };
124 #endif
#define NULL
Definition: coverity.c:32
const uint8_t * buffer
Definition: get_bits.h:55
int frames
Definition: movenc-test.c:65
#define av_assert0(cond)
assert() equivalent, that is always enabled.
Definition: avassert.h:37
uint8_t
static av_cold int end(AVCodecContext *avctx)
Definition: avrndec.c:90
uint8_t bitstream_id
Definition: ac3.h:183
AVCodecID
Identify the syntax and semantics of the bitstream.
Definition: avcodec.h:101
int ff_raw_read_partial_packet(AVFormatContext *s, AVPacket *pkt)
Definition: rawdec.c:35
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification. ...
Definition: internal.h:176
Coded AC-3 header values up to the lfeon element, plus derived values.
Definition: ac3.h:176
enum AVCodecID codec_id
Definition: mov_chan.c:433
#define FFMAX(a, b)
Definition: common.h:94
int buf_size
Size of buf except extra allocated bytes.
Definition: avformat.h:463
unsigned char * buf
Buffer must have AVPROBE_PADDING_SIZE of extra allocated bytes filled with zero.
Definition: avformat.h:462
int ff_raw_audio_read_header(AVFormatContext *s)
Definition: rawdec.c:55
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:356
static int ac3_eac3_probe(AVProbeData *p, enum AVCodecID expected_codec_id)
Definition: ac3dec.c:27
void * buf
Definition: avisynth_c.h:553
#define AVFMT_GENERIC_INDEX
Use generic index building code.
Definition: avformat.h:486
static int init_get_bits(GetBitContext *s, const uint8_t *buffer, int bit_size)
Initialize GetBitContext.
Definition: get_bits.h:418
uint16_t frame_size
Definition: ac3.h:203
#define AVPROBE_SCORE_EXTENSION
score for file extension
Definition: avformat.h:470
This structure contains the data a format has to probe a file.
Definition: avformat.h:460
const AVCRC * av_crc_get_table(AVCRCId crc_id)
Get an initialized standard CRC table.
Definition: crc.c:342
Main libavformat public API header.
int avpriv_ac3_parse_header(GetBitContext *gbc, AC3HeaderInfo **phdr)
Parse AC-3 frame header.
Definition: ac3_parser.c:50
#define av_freep(p)
const char * name
A comma separated list of short names for the format.
Definition: avformat.h:661