]> git.sesse.net Git - ffmpeg/blob - libavcodec/aac_adtstoasc_bsf.c
pthread_frame: ensure the threads don't run simultaneously with hwaccel
[ffmpeg] / libavcodec / aac_adtstoasc_bsf.c
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 "bsf.h"
25 #include "put_bits.h"
26 #include "get_bits.h"
27 #include "mpeg4audio.h"
28 #include "internal.h"
29
30 typedef struct AACBSFContext {
31     int first_frame_done;
32 } AACBSFContext;
33
34 /**
35  * This filter creates an MPEG-4 AudioSpecificConfig from an MPEG-2/4
36  * ADTS header and removes the ADTS header.
37  */
38 static int aac_adtstoasc_filter(AVBSFContext *bsfc, AVPacket *out)
39 {
40     AACBSFContext *ctx = bsfc->priv_data;
41
42     GetBitContext gb;
43     PutBitContext pb;
44     AACADTSHeaderInfo hdr;
45     AVPacket *in;
46     int ret;
47
48     ret = ff_bsf_get_packet(bsfc, &in);
49     if (ret < 0)
50         return ret;
51
52     if (in->size < AAC_ADTS_HEADER_SIZE)
53         goto packet_too_small;
54
55     init_get_bits(&gb, in->data, AAC_ADTS_HEADER_SIZE * 8);
56
57     if (bsfc->par_in->extradata && show_bits(&gb, 12) != 0xfff)
58         goto finish;
59
60     if (avpriv_aac_parse_header(&gb, &hdr) < 0) {
61         av_log(bsfc, AV_LOG_ERROR, "Error parsing ADTS frame header!\n");
62         ret = AVERROR_INVALIDDATA;
63         goto fail;
64     }
65
66     if (!hdr.crc_absent && hdr.num_aac_frames > 1) {
67         avpriv_report_missing_feature(bsfc,
68                                       "Multiple RDBs per frame with CRC");
69         ret = AVERROR_PATCHWELCOME;
70         goto fail;
71     }
72
73     in->size -= AAC_ADTS_HEADER_SIZE + 2 * !hdr.crc_absent;
74     if (in->size <= 0)
75         goto packet_too_small;
76     in->data += AAC_ADTS_HEADER_SIZE + 2 * !hdr.crc_absent;
77
78     if (!ctx->first_frame_done) {
79         int            pce_size = 0;
80         uint8_t        pce_data[MAX_PCE_SIZE];
81         uint8_t       *extradata;
82
83         if (!hdr.chan_config) {
84             init_get_bits(&gb, in->data, in->size * 8);
85             if (get_bits(&gb, 3) != 5) {
86                 avpriv_report_missing_feature(bsfc,
87                                               "PCE-based channel configuration "
88                                               "without PCE as first syntax "
89                                               "element");
90                 ret = AVERROR_PATCHWELCOME;
91                 goto fail;
92             }
93             init_put_bits(&pb, pce_data, MAX_PCE_SIZE);
94             pce_size = avpriv_copy_pce_data(&pb, &gb)/8;
95             flush_put_bits(&pb);
96             in->size -= get_bits_count(&gb)/8;
97             in->data += get_bits_count(&gb)/8;
98         }
99
100         extradata = av_mallocz(2 + pce_size + AV_INPUT_BUFFER_PADDING_SIZE);
101         if (!extradata) {
102             ret = AVERROR(ENOMEM);
103             goto fail;
104         }
105
106         init_put_bits(&pb, extradata, 2 + pce_size);
107         put_bits(&pb, 5, hdr.object_type);
108         put_bits(&pb, 4, hdr.sampling_index);
109         put_bits(&pb, 4, hdr.chan_config);
110         put_bits(&pb, 1, 0); //frame length - 1024 samples
111         put_bits(&pb, 1, 0); //does not depend on core coder
112         put_bits(&pb, 1, 0); //is not extension
113         flush_put_bits(&pb);
114         if (pce_size) {
115             memcpy(extradata + 2, pce_data, pce_size);
116         }
117
118         bsfc->par_out->extradata = extradata;
119         bsfc->par_out->extradata_size = 2 + pce_size;
120         ctx->first_frame_done = 1;
121     }
122
123 finish:
124     av_packet_move_ref(out, in);
125     av_packet_free(&in);
126
127     return 0;
128
129 packet_too_small:
130     av_log(bsfc, AV_LOG_ERROR, "Input packet too small\n");
131     ret = AVERROR_INVALIDDATA;
132 fail:
133     av_packet_free(&in);
134     return ret;
135 }
136
137 static int aac_adtstoasc_init(AVBSFContext *ctx)
138 {
139     /* Validate the extradata if the stream is already MPEG-4 AudioSpecificConfig */
140     if (ctx->par_in->extradata) {
141         MPEG4AudioConfig mp4ac;
142         int ret = avpriv_mpeg4audio_get_config(&mp4ac, ctx->par_in->extradata,
143                                                ctx->par_in->extradata_size * 8, 1);
144         if (ret < 0) {
145             av_log(ctx, AV_LOG_ERROR, "Error parsing AudioSpecificConfig extradata!\n");
146             return ret;
147         }
148     }
149
150     return 0;
151 }
152
153 static const enum AVCodecID codec_ids[] = {
154     AV_CODEC_ID_AAC, AV_CODEC_ID_NONE,
155 };
156
157 const AVBitStreamFilter ff_aac_adtstoasc_bsf = {
158     .name           = "aac_adtstoasc",
159     .priv_data_size = sizeof(AACBSFContext),
160     .init           = aac_adtstoasc_init,
161     .filter         = aac_adtstoasc_filter,
162     .codec_ids      = codec_ids,
163 };