FFmpeg
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
aac_adtstoasc_bsf.c
Go to the documentation of this file.
1 /*
2  * MPEG-2/4 AAC ADTS to MPEG-4 Audio Specific Configuration bitstream filter
3  * Copyright (c) 2009 Alex Converse <alex.converse@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 "avcodec.h"
23 #include "aacadtsdec.h"
24 #include "put_bits.h"
25 #include "get_bits.h"
26 #include "mpeg4audio.h"
27 #include "internal.h"
28 
29 typedef struct AACBSFContext {
32 
33 /**
34  * This filter creates an MPEG-4 AudioSpecificConfig from an MPEG-2/4
35  * ADTS header and removes the ADTS header.
36  */
38  AVCodecContext *avctx, const char *args,
39  uint8_t **poutbuf, int *poutbuf_size,
40  const uint8_t *buf, int buf_size,
41  int keyframe)
42 {
43  GetBitContext gb;
44  PutBitContext pb;
46 
47  AACBSFContext *ctx = bsfc->priv_data;
48 
50 
51  *poutbuf = (uint8_t*) buf;
52  *poutbuf_size = buf_size;
53 
54  if (avctx->extradata)
55  if (show_bits(&gb, 12) != 0xfff)
56  return 0;
57 
58  if (avpriv_aac_parse_header(&gb, &hdr) < 0) {
59  av_log(avctx, AV_LOG_ERROR, "Error parsing ADTS frame header!\n");
60  return AVERROR_INVALIDDATA;
61  }
62 
63  if (!hdr.crc_absent && hdr.num_aac_frames > 1) {
65  "Multiple RDBs per frame with CRC");
66  return AVERROR_PATCHWELCOME;
67  }
68 
69  buf += AAC_ADTS_HEADER_SIZE + 2*!hdr.crc_absent;
70  buf_size -= AAC_ADTS_HEADER_SIZE + 2*!hdr.crc_absent;
71 
72  if (!ctx->first_frame_done) {
73  int pce_size = 0;
74  uint8_t pce_data[MAX_PCE_SIZE];
75  if (!hdr.chan_config) {
76  init_get_bits(&gb, buf, buf_size * 8);
77  if (get_bits(&gb, 3) != 5) {
79  "PCE-based channel configuration "
80  "without PCE as first syntax "
81  "element");
82  return AVERROR_PATCHWELCOME;
83  }
84  init_put_bits(&pb, pce_data, MAX_PCE_SIZE);
85  pce_size = avpriv_copy_pce_data(&pb, &gb)/8;
86  flush_put_bits(&pb);
87  buf_size -= get_bits_count(&gb)/8;
88  buf += get_bits_count(&gb)/8;
89  }
90  av_free(avctx->extradata);
91  avctx->extradata_size = 2 + pce_size;
93  if (!avctx->extradata) {
94  avctx->extradata_size = 0;
95  return AVERROR(ENOMEM);
96  }
97 
98  init_put_bits(&pb, avctx->extradata, avctx->extradata_size);
99  put_bits(&pb, 5, hdr.object_type);
100  put_bits(&pb, 4, hdr.sampling_index);
101  put_bits(&pb, 4, hdr.chan_config);
102  put_bits(&pb, 1, 0); //frame length - 1024 samples
103  put_bits(&pb, 1, 0); //does not depend on core coder
104  put_bits(&pb, 1, 0); //is not extension
105  flush_put_bits(&pb);
106  if (pce_size) {
107  memcpy(avctx->extradata + 2, pce_data, pce_size);
108  }
109 
110  ctx->first_frame_done = 1;
111  }
112 
113  *poutbuf = (uint8_t*) buf;
114  *poutbuf_size = buf_size;
115 
116  return 0;
117 }
118 
120  .name = "aac_adtstoasc",
121  .priv_data_size = sizeof(AACBSFContext),
123 };
static int aac_adtstoasc_filter(AVBitStreamFilterContext *bsfc, AVCodecContext *avctx, const char *args, uint8_t **poutbuf, int *poutbuf_size, const uint8_t *buf, int buf_size, int keyframe)
This filter creates an MPEG-4 AudioSpecificConfig from an MPEG-2/4 ADTS header and removes the ADTS h...
#define AVERROR_INVALIDDATA
Invalid data found when processing input.
Definition: error.h:59
uint8_t object_type
Definition: aacadtsdec.h:36
AVFormatContext * ctx
Definition: movenc-test.c:48
static void put_bits(Jpeg2000EncoderContext *s, int val, int n)
put n times val bit
Definition: j2kenc.c:168
static unsigned int get_bits(GetBitContext *s, int n)
Read 1-25 bits.
Definition: get_bits.h:260
uint8_t
uint8_t * extradata
some codecs need / can use extradata like Huffman tables.
Definition: avcodec.h:1647
static void filter(int16_t *output, ptrdiff_t out_stride, int16_t *low, ptrdiff_t low_stride, int16_t *high, ptrdiff_t high_stride, int len, uint8_t clip)
Definition: cfhd.c:82
const char * name
Definition: avcodec.h:5152
static int get_bits_count(const GetBitContext *s)
Definition: get_bits.h:212
bitstream reader API header.
#define av_log(a,...)
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:176
#define AVERROR(e)
Definition: error.h:43
uint8_t sampling_index
Definition: aacadtsdec.h:37
uint8_t chan_config
Definition: aacadtsdec.h:38
uint8_t num_aac_frames
Definition: aacadtsdec.h:39
static unsigned int show_bits(GetBitContext *s, int n)
Show 1-25 bits.
Definition: get_bits.h:295
#define AVERROR_PATCHWELCOME
Not yet implemented in FFmpeg, patches welcome.
Definition: error.h:62
#define AAC_ADTS_HEADER_SIZE
Definition: aacadtsdec.h:29
Libavcodec external API header.
main external API structure.
Definition: avcodec.h:1532
int avpriv_aac_parse_header(GetBitContext *gbc, AACADTSHeaderInfo *hdr)
Parse AAC frame header.
Definition: aacadtsdec.c:29
void * buf
Definition: avisynth_c.h:553
int extradata_size
Definition: avcodec.h:1648
static int init_get_bits(GetBitContext *s, const uint8_t *buffer, int bit_size)
Initialize GetBitContext.
Definition: get_bits.h:418
void avpriv_report_missing_feature(void *avc, const char *msg,...) av_printf_format(2
Log a generic warning message about a missing feature.
common internal api header.
static void flush_put_bits(PutBitContext *s)
Pad the end of the output stream with zeros.
Definition: put_bits.h:101
static void init_put_bits(PutBitContext *s, uint8_t *buffer, int buffer_size)
Initialize the PutBitContext s.
Definition: put_bits.h:48
#define MAX_PCE_SIZE
Maximum size of a PCE including the 3-bit ID_PCE.
Definition: mpeg4audio.h:105
#define AV_INPUT_BUFFER_PADDING_SIZE
Required number of additionally allocated bytes at the end of the input bitstream for decoding...
Definition: avcodec.h:635
#define av_free(p)
AVBitStreamFilter ff_aac_adtstoasc_bsf
int avpriv_copy_pce_data(PutBitContext *pb, GetBitContext *gb)
Definition: mpeg4audio.c:166
uint8_t crc_absent
Definition: aacadtsdec.h:35
void * av_mallocz(size_t size)
Allocate a block of size bytes with alignment suitable for all memory accesses (including vectors if ...
Definition: mem.c:252
bitstream writer API