FFmpeg
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 "config_components.h"
23 
24 #include "libavutil/avassert.h"
25 #include "libavutil/crc.h"
26 #include "libavcodec/ac3_parser.h"
27 #include "avformat.h"
28 #include "rawdec.h"
29 
30 static int ac3_eac3_probe(const AVProbeData *p, enum AVCodecID expected_codec_id)
31 {
32  int max_frames, first_frames = 0, frames;
33  const uint8_t *buf, *buf2, *end;
35 
36  max_frames = 0;
37  buf = p->buf;
38  end = buf + p->buf_size;
39 
40  for(; buf < end; buf++) {
41  if(buf > p->buf && !(buf[0] == 0x0B && buf[1] == 0x77)
42  && !(buf[0] == 0x77 && buf[1] == 0x0B) )
43  continue;
44  buf2 = buf;
45 
46  for(frames = 0; buf2 < end; frames++) {
47  uint8_t buf3[4096];
48  uint8_t bitstream_id;
49  uint16_t frame_size;
50  int i, ret;
51 
52  if(!memcmp(buf2, "\x1\x10", 2)) {
53  if (buf2 + 16 > end)
54  break;
55  buf2+=16;
56  }
57  if (buf[0] == 0x77 && buf[1] == 0x0B) {
58  for(i=0; i<8; i+=2) {
59  buf3[i ] = buf2[i+1];
60  buf3[i+1] = buf2[i ];
61  }
62  ret = av_ac3_parse_header(buf3, 8, &bitstream_id,
63  &frame_size);
64  }else
65  ret = av_ac3_parse_header(buf2, end - buf2, &bitstream_id,
66  &frame_size);
67  if (ret < 0)
68  break;
69  if(buf2 + frame_size > end)
70  break;
71  if (buf[0] == 0x77 && buf[1] == 0x0B) {
72  av_assert0(frame_size <= sizeof(buf3));
73  for(i = 8; i < frame_size; i += 2) {
74  buf3[i ] = buf2[i+1];
75  buf3[i+1] = buf2[i ];
76  }
77  if (av_crc(av_crc_get_table(AV_CRC_16_ANSI), 0, buf3 + 2, frame_size - 2))
78  break;
79  } else {
80  if (av_crc(av_crc_get_table(AV_CRC_16_ANSI), 0, buf2 + 2, frame_size - 2))
81  break;
82  }
83  if (bitstream_id > 10)
85  buf2 += frame_size;
86  }
87  max_frames = FFMAX(max_frames, frames);
88  if(buf == p->buf)
89  first_frames = frames;
90  }
91  if(codec_id != expected_codec_id) return 0;
92  // keep this in sync with mp3 probe, both need to avoid
93  // issues with MPEG-files!
94  if (first_frames>=7) return AVPROBE_SCORE_EXTENSION + 1;
95  else if(max_frames>200)return AVPROBE_SCORE_EXTENSION;
96  else if(max_frames>=4) return AVPROBE_SCORE_EXTENSION/2;
97  else if(max_frames>=1) return 1;
98  else return 0;
99 }
100 
101 #if CONFIG_AC3_DEMUXER
102 static int ac3_probe(const AVProbeData *p)
103 {
104  return ac3_eac3_probe(p, AV_CODEC_ID_AC3);
105 }
106 
108  .name = "ac3",
109  .long_name = NULL_IF_CONFIG_SMALL("raw AC-3"),
110  .read_probe = ac3_probe,
111  .read_header = ff_raw_audio_read_header,
112  .read_packet = ff_raw_read_partial_packet,
113  .flags= AVFMT_GENERIC_INDEX,
114  .extensions = "ac3",
115  .raw_codec_id = AV_CODEC_ID_AC3,
116  .priv_data_size = sizeof(FFRawDemuxerContext),
117  .priv_class = &ff_raw_demuxer_class,
118 };
119 #endif
120 
121 #if CONFIG_EAC3_DEMUXER
122 static int eac3_probe(const AVProbeData *p)
123 {
124  return ac3_eac3_probe(p, AV_CODEC_ID_EAC3);
125 }
126 
128  .name = "eac3",
129  .long_name = NULL_IF_CONFIG_SMALL("raw E-AC-3"),
130  .read_probe = eac3_probe,
131  .read_header = ff_raw_audio_read_header,
132  .read_packet = ff_raw_read_partial_packet,
133  .flags = AVFMT_GENERIC_INDEX,
134  .extensions = "eac3",
135  .raw_codec_id = AV_CODEC_ID_EAC3,
136  .priv_data_size = sizeof(FFRawDemuxerContext),
137  .priv_class = &ff_raw_demuxer_class,
138 };
139 #endif
AV_CODEC_ID_AC3
@ AV_CODEC_ID_AC3
Definition: codec_id.h:430
ac3_eac3_probe
static int ac3_eac3_probe(const AVProbeData *p, enum AVCodecID expected_codec_id)
Definition: ac3dec.c:30
AVProbeData::buf_size
int buf_size
Size of buf except extra allocated bytes.
Definition: avformat.h:456
FFMAX
#define FFMAX(a, b)
Definition: macros.h:47
ac3_parser.h
av_ac3_parse_header
int av_ac3_parse_header(const uint8_t *buf, size_t size, uint8_t *bitstream_id, uint16_t *frame_size)
Extract the bitstream ID and the frame size from AC-3 data.
Definition: ac3_parser.c:265
crc.h
frames
if it could not because there are no more frames
Definition: filter_design.txt:266
FFRawDemuxerContext
Definition: rawdec.h:36
AVFMT_GENERIC_INDEX
#define AVFMT_GENERIC_INDEX
Use generic index building code.
Definition: avformat.h:482
avassert.h
AVInputFormat
Definition: avformat.h:656
ff_raw_read_partial_packet
int ff_raw_read_partial_packet(AVFormatContext *s, AVPacket *pkt)
Definition: rawdec.c:36
AVInputFormat::name
const char * name
A comma separated list of short names for the format.
Definition: avformat.h:661
AVProbeData::buf
unsigned char * buf
Buffer must have AVPROBE_PADDING_SIZE of extra allocated bytes filled with zero.
Definition: avformat.h:455
frame_size
int frame_size
Definition: mxfenc.c:2201
av_assert0
#define av_assert0(cond)
assert() equivalent, that is always enabled.
Definition: avassert.h:37
codec_id
enum AVCodecID codec_id
Definition: vaapi_decode.c:371
ff_raw_demuxer_class
const AVClass ff_raw_demuxer_class
Definition: rawdec.c:144
AV_CRC_16_ANSI
@ AV_CRC_16_ANSI
Definition: crc.h:50
rawdec.h
ff_raw_audio_read_header
int ff_raw_audio_read_header(AVFormatContext *s)
Definition: rawdec.c:57
AVProbeData
This structure contains the data a format has to probe a file.
Definition: avformat.h:453
ff_eac3_demuxer
const AVInputFormat ff_eac3_demuxer
AVPROBE_SCORE_EXTENSION
#define AVPROBE_SCORE_EXTENSION
score for file extension
Definition: avformat.h:463
AVCodecID
AVCodecID
Identify the syntax and semantics of the bitstream.
Definition: codec_id.h:47
AV_CODEC_ID_EAC3
@ AV_CODEC_ID_EAC3
Definition: codec_id.h:467
NULL_IF_CONFIG_SMALL
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification.
Definition: internal.h:117
av_crc_get_table
const AVCRC * av_crc_get_table(AVCRCId crc_id)
Get an initialized standard CRC table.
Definition: crc.c:374
i
#define i(width, name, range_min, range_max)
Definition: cbs_h2645.c:269
ret
ret
Definition: filter_design.txt:187
avformat.h
av_crc
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:392
ff_ac3_demuxer
const AVInputFormat ff_ac3_demuxer