/src/ffmpeg/libavcodec/aac_ac3_parser.c
Line | Count | Source |
1 | | /* |
2 | | * Common AAC and AC-3 parser |
3 | | * Copyright (c) 2003 Fabrice Bellard |
4 | | * Copyright (c) 2003 Michael Niedermayer |
5 | | * |
6 | | * This file is part of FFmpeg. |
7 | | * |
8 | | * FFmpeg is free software; you can redistribute it and/or |
9 | | * modify it under the terms of the GNU Lesser General Public |
10 | | * License as published by the Free Software Foundation; either |
11 | | * version 2.1 of the License, or (at your option) any later version. |
12 | | * |
13 | | * FFmpeg is distributed in the hope that it will be useful, |
14 | | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
15 | | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
16 | | * Lesser General Public License for more details. |
17 | | * |
18 | | * You should have received a copy of the GNU Lesser General Public |
19 | | * License along with FFmpeg; if not, write to the Free Software |
20 | | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
21 | | */ |
22 | | |
23 | | #include "config_components.h" |
24 | | |
25 | | #include "libavutil/channel_layout.h" |
26 | | #include "libavutil/common.h" |
27 | | #include "parser.h" |
28 | | #include "aac_ac3_parser.h" |
29 | | #include "ac3_parser_internal.h" |
30 | | #include "adts_header.h" |
31 | | |
32 | | int ff_aac_ac3_parse(AVCodecParserContext *s1, |
33 | | AVCodecContext *avctx, |
34 | | const uint8_t **poutbuf, int *poutbuf_size, |
35 | | const uint8_t *buf, int buf_size) |
36 | 16.3M | { |
37 | 16.3M | AACAC3ParseContext *s = s1->priv_data; |
38 | 16.3M | ParseContext *pc = &s->pc; |
39 | 16.3M | int len, i; |
40 | 16.3M | int new_frame_start; |
41 | 16.3M | int got_frame = 0; |
42 | | |
43 | 16.3M | s1->key_frame = -1; |
44 | | |
45 | 16.3M | if (s1->flags & PARSER_FLAG_COMPLETE_FRAMES) { |
46 | 587k | i = buf_size; |
47 | 587k | got_frame = 1; |
48 | 15.7M | } else { |
49 | 16.1M | get_next: |
50 | 16.1M | i=END_NOT_FOUND; |
51 | 16.1M | if(s->remaining_size <= buf_size){ |
52 | 15.5M | if(s->remaining_size && !s->need_next_header){ |
53 | 2.49M | i= s->remaining_size; |
54 | 2.49M | s->remaining_size = 0; |
55 | 13.0M | }else{ //we need a header first |
56 | 13.0M | len=0; |
57 | 911M | for(i=s->remaining_size; i<buf_size; i++){ |
58 | 905M | s->state = (s->state<<8) + buf[i]; |
59 | 905M | if((len=s->sync(s->state, &s->need_next_header, &new_frame_start))) |
60 | 7.23M | break; |
61 | 905M | } |
62 | 13.0M | if(len<=0){ |
63 | 5.78M | i=END_NOT_FOUND; |
64 | 7.23M | }else{ |
65 | 7.23M | got_frame = 1; |
66 | 7.23M | s->state=0; |
67 | 7.23M | i-= s->header_size -1; |
68 | 7.23M | s->remaining_size = len; |
69 | 7.23M | if(!new_frame_start || pc->index+i<=0){ |
70 | 397k | s->remaining_size += i; |
71 | 397k | goto get_next; |
72 | 397k | } |
73 | 6.83M | else if (i < 0) { |
74 | 56.5k | s->remaining_size += i; |
75 | 56.5k | } |
76 | 7.23M | } |
77 | 13.0M | } |
78 | 15.5M | } |
79 | | |
80 | 15.7M | if(ff_combine_frame(pc, i, &buf, &buf_size)<0){ |
81 | 6.37M | s->remaining_size -= FFMIN(s->remaining_size, buf_size); |
82 | 6.37M | *poutbuf = NULL; |
83 | 6.37M | *poutbuf_size = 0; |
84 | 6.37M | return buf_size; |
85 | 6.37M | } |
86 | 15.7M | } |
87 | | |
88 | 9.99M | *poutbuf = buf; |
89 | 9.99M | *poutbuf_size = buf_size; |
90 | | |
91 | 9.99M | if (got_frame) { |
92 | 7.47M | int bit_rate; |
93 | | |
94 | | /* Due to backwards compatible HE-AAC the sample rate, channel count, |
95 | | and total number of samples found in an AAC ADTS header are not |
96 | | reliable. Bit rate is still accurate because the total frame |
97 | | duration in seconds is still correct (as is the number of bits in |
98 | | the frame). */ |
99 | 7.47M | if (avctx->codec_id != AV_CODEC_ID_AAC) { |
100 | 4.97M | #if CONFIG_AC3_PARSER |
101 | 4.97M | AC3HeaderInfo hdr, *phrd = &hdr; |
102 | 4.97M | int offset = ff_ac3_find_syncword(buf, buf_size); |
103 | | |
104 | 4.97M | if (offset < 0) |
105 | 528k | return i; |
106 | | |
107 | 4.45M | buf += offset; |
108 | 4.45M | buf_size -= offset; |
109 | 5.26M | while (buf_size > 0) { |
110 | 5.26M | int ret = avpriv_ac3_parse_header(&phrd, buf, buf_size); |
111 | | |
112 | 5.26M | if (ret < 0 || hdr.frame_size > buf_size) |
113 | 3.91M | return i; |
114 | | |
115 | 1.35M | if (buf_size > hdr.frame_size) { |
116 | 815k | buf += hdr.frame_size; |
117 | 815k | buf_size -= hdr.frame_size; |
118 | 815k | continue; |
119 | 815k | } |
120 | | /* Check for false positives since the syncword is not enough. |
121 | | See section 6.1.2 of A/52. */ |
122 | 537k | if (av_crc(s->crc_ctx, 0, buf + 2, hdr.frame_size - 2)) |
123 | 478k | return i; |
124 | 58.9k | break; |
125 | 537k | } |
126 | | |
127 | 58.9k | avctx->sample_rate = hdr.sample_rate; |
128 | | |
129 | 58.9k | if (hdr.bitstream_id > 10) |
130 | 48.1k | avctx->codec_id = AV_CODEC_ID_EAC3; |
131 | | |
132 | 58.9k | if (!CONFIG_EAC3_DECODER || avctx->codec_id != AV_CODEC_ID_EAC3) { |
133 | 51.4k | av_channel_layout_uninit(&avctx->ch_layout); |
134 | 51.4k | if (hdr.channel_layout) { |
135 | 51.4k | av_channel_layout_from_mask(&avctx->ch_layout, hdr.channel_layout); |
136 | 51.4k | } else { |
137 | 0 | avctx->ch_layout.order = AV_CHANNEL_ORDER_UNSPEC; |
138 | 0 | avctx->ch_layout.nb_channels = hdr.channels; |
139 | 0 | } |
140 | 51.4k | } |
141 | 58.9k | s1->duration = hdr.num_blocks * 256; |
142 | 58.9k | avctx->audio_service_type = hdr.bitstream_mode; |
143 | 58.9k | if (hdr.bitstream_mode == 0x7 && hdr.channels > 1) |
144 | 3.87k | avctx->audio_service_type = AV_AUDIO_SERVICE_TYPE_KARAOKE; |
145 | 58.9k | bit_rate = hdr.bit_rate; |
146 | 58.9k | #endif |
147 | 2.49M | } else { |
148 | | #if CONFIG_AAC_PARSER |
149 | | AACADTSHeaderInfo hdr; |
150 | | |
151 | 2.49M | if (buf_size < AV_AAC_ADTS_HEADER_SIZE || |
152 | 2.13M | ff_adts_header_parse_buf(buf, &hdr) < 0) |
153 | 2.44M | return i; |
154 | | |
155 | 55.8k | if (avctx->profile == AV_PROFILE_UNKNOWN) |
156 | 2.54k | avctx->profile = hdr.object_type - 1; |
157 | | /* ADTS does not support USAC */ |
158 | | s1->key_frame = 1; |
159 | | bit_rate = hdr.bit_rate; |
160 | | #endif |
161 | 55.8k | } |
162 | | |
163 | | /* Calculate the average bit rate */ |
164 | 114k | s->frame_number++; |
165 | 114k | if (!CONFIG_EAC3_DECODER || avctx->codec_id != AV_CODEC_ID_EAC3) { |
166 | 107k | avctx->bit_rate += |
167 | 107k | (bit_rate - avctx->bit_rate) / s->frame_number; |
168 | 107k | } |
169 | 114k | } |
170 | | |
171 | 2.62M | return i; |
172 | 9.99M | } Line | Count | Source | 36 | 6.77M | { | 37 | 6.77M | AACAC3ParseContext *s = s1->priv_data; | 38 | 6.77M | ParseContext *pc = &s->pc; | 39 | 6.77M | int len, i; | 40 | 6.77M | int new_frame_start; | 41 | 6.77M | int got_frame = 0; | 42 | | | 43 | 6.77M | s1->key_frame = -1; | 44 | | | 45 | 6.77M | if (s1->flags & PARSER_FLAG_COMPLETE_FRAMES) { | 46 | 5.29k | i = buf_size; | 47 | 5.29k | got_frame = 1; | 48 | 6.76M | } else { | 49 | 7.10M | get_next: | 50 | 7.10M | i=END_NOT_FOUND; | 51 | 7.10M | if(s->remaining_size <= buf_size){ | 52 | 6.77M | if(s->remaining_size && !s->need_next_header){ | 53 | 2.49M | i= s->remaining_size; | 54 | 2.49M | s->remaining_size = 0; | 55 | 4.28M | }else{ //we need a header first | 56 | 4.28M | len=0; | 57 | 306M | for(i=s->remaining_size; i<buf_size; i++){ | 58 | 306M | s->state = (s->state<<8) + buf[i]; | 59 | 306M | if((len=s->sync(s->state, &s->need_next_header, &new_frame_start))) | 60 | 3.95M | break; | 61 | 306M | } | 62 | 4.28M | if(len<=0){ | 63 | 327k | i=END_NOT_FOUND; | 64 | 3.95M | }else{ | 65 | 3.95M | got_frame = 1; | 66 | 3.95M | s->state=0; | 67 | 3.95M | i-= s->header_size -1; | 68 | 3.95M | s->remaining_size = len; | 69 | 3.95M | if(!new_frame_start || pc->index+i<=0){ | 70 | 333k | s->remaining_size += i; | 71 | 333k | goto get_next; | 72 | 333k | } | 73 | 3.61M | else if (i < 0) { | 74 | 8.54k | s->remaining_size += i; | 75 | 8.54k | } | 76 | 3.95M | } | 77 | 4.28M | } | 78 | 6.77M | } | 79 | | | 80 | 6.76M | if(ff_combine_frame(pc, i, &buf, &buf_size)<0){ | 81 | 649k | s->remaining_size -= FFMIN(s->remaining_size, buf_size); | 82 | 649k | *poutbuf = NULL; | 83 | 649k | *poutbuf_size = 0; | 84 | 649k | return buf_size; | 85 | 649k | } | 86 | 6.76M | } | 87 | | | 88 | 6.12M | *poutbuf = buf; | 89 | 6.12M | *poutbuf_size = buf_size; | 90 | | | 91 | 6.12M | if (got_frame) { | 92 | 3.68M | int bit_rate; | 93 | | | 94 | | /* Due to backwards compatible HE-AAC the sample rate, channel count, | 95 | | and total number of samples found in an AAC ADTS header are not | 96 | | reliable. Bit rate is still accurate because the total frame | 97 | | duration in seconds is still correct (as is the number of bits in | 98 | | the frame). */ | 99 | 3.68M | if (avctx->codec_id != AV_CODEC_ID_AAC) { | 100 | 1.18M | #if CONFIG_AC3_PARSER | 101 | 1.18M | AC3HeaderInfo hdr, *phrd = &hdr; | 102 | 1.18M | int offset = ff_ac3_find_syncword(buf, buf_size); | 103 | | | 104 | 1.18M | if (offset < 0) | 105 | 4.21k | return i; | 106 | | | 107 | 1.17M | buf += offset; | 108 | 1.17M | buf_size -= offset; | 109 | 1.54M | while (buf_size > 0) { | 110 | 1.54M | int ret = avpriv_ac3_parse_header(&phrd, buf, buf_size); | 111 | | | 112 | 1.54M | if (ret < 0 || hdr.frame_size > buf_size) | 113 | 872k | return i; | 114 | | | 115 | 670k | if (buf_size > hdr.frame_size) { | 116 | 365k | buf += hdr.frame_size; | 117 | 365k | buf_size -= hdr.frame_size; | 118 | 365k | continue; | 119 | 365k | } | 120 | | /* Check for false positives since the syncword is not enough. | 121 | | See section 6.1.2 of A/52. */ | 122 | 304k | if (av_crc(s->crc_ctx, 0, buf + 2, hdr.frame_size - 2)) | 123 | 293k | return i; | 124 | 11.1k | break; | 125 | 304k | } | 126 | | | 127 | 11.1k | avctx->sample_rate = hdr.sample_rate; | 128 | | | 129 | 11.1k | if (hdr.bitstream_id > 10) | 130 | 6.61k | avctx->codec_id = AV_CODEC_ID_EAC3; | 131 | | | 132 | 11.1k | if (!CONFIG_EAC3_DECODER || avctx->codec_id != AV_CODEC_ID_EAC3) { | 133 | 3.71k | av_channel_layout_uninit(&avctx->ch_layout); | 134 | 3.71k | if (hdr.channel_layout) { | 135 | 3.71k | av_channel_layout_from_mask(&avctx->ch_layout, hdr.channel_layout); | 136 | 3.71k | } else { | 137 | 0 | avctx->ch_layout.order = AV_CHANNEL_ORDER_UNSPEC; | 138 | 0 | avctx->ch_layout.nb_channels = hdr.channels; | 139 | 0 | } | 140 | 3.71k | } | 141 | 11.1k | s1->duration = hdr.num_blocks * 256; | 142 | 11.1k | avctx->audio_service_type = hdr.bitstream_mode; | 143 | 11.1k | if (hdr.bitstream_mode == 0x7 && hdr.channels > 1) | 144 | 1.49k | avctx->audio_service_type = AV_AUDIO_SERVICE_TYPE_KARAOKE; | 145 | 11.1k | bit_rate = hdr.bit_rate; | 146 | 11.1k | #endif | 147 | 2.49M | } else { | 148 | 2.49M | #if CONFIG_AAC_PARSER | 149 | 2.49M | AACADTSHeaderInfo hdr; | 150 | | | 151 | 2.49M | if (buf_size < AV_AAC_ADTS_HEADER_SIZE || | 152 | 2.13M | ff_adts_header_parse_buf(buf, &hdr) < 0) | 153 | 2.44M | return i; | 154 | | | 155 | 55.8k | if (avctx->profile == AV_PROFILE_UNKNOWN) | 156 | 2.54k | avctx->profile = hdr.object_type - 1; | 157 | | /* ADTS does not support USAC */ | 158 | 55.8k | s1->key_frame = 1; | 159 | 55.8k | bit_rate = hdr.bit_rate; | 160 | 55.8k | #endif | 161 | 55.8k | } | 162 | | | 163 | | /* Calculate the average bit rate */ | 164 | 66.9k | s->frame_number++; | 165 | 66.9k | if (!CONFIG_EAC3_DECODER || avctx->codec_id != AV_CODEC_ID_EAC3) { | 166 | 59.5k | avctx->bit_rate += | 167 | 59.5k | (bit_rate - avctx->bit_rate) / s->frame_number; | 168 | 59.5k | } | 169 | 66.9k | } | 170 | | | 171 | 2.51M | return i; | 172 | 6.12M | } |
Line | Count | Source | 36 | 9.59M | { | 37 | 9.59M | AACAC3ParseContext *s = s1->priv_data; | 38 | 9.59M | ParseContext *pc = &s->pc; | 39 | 9.59M | int len, i; | 40 | 9.59M | int new_frame_start; | 41 | 9.59M | int got_frame = 0; | 42 | | | 43 | 9.59M | s1->key_frame = -1; | 44 | | | 45 | 9.59M | if (s1->flags & PARSER_FLAG_COMPLETE_FRAMES) { | 46 | 581k | i = buf_size; | 47 | 581k | got_frame = 1; | 48 | 9.00M | } else { | 49 | 9.07M | get_next: | 50 | 9.07M | i=END_NOT_FOUND; | 51 | 9.07M | if(s->remaining_size <= buf_size){ | 52 | 8.74M | if(s->remaining_size && !s->need_next_header){ | 53 | 0 | i= s->remaining_size; | 54 | 0 | s->remaining_size = 0; | 55 | 8.74M | }else{ //we need a header first | 56 | 8.74M | len=0; | 57 | 604M | for(i=s->remaining_size; i<buf_size; i++){ | 58 | 599M | s->state = (s->state<<8) + buf[i]; | 59 | 599M | if((len=s->sync(s->state, &s->need_next_header, &new_frame_start))) | 60 | 3.28M | break; | 61 | 599M | } | 62 | 8.74M | if(len<=0){ | 63 | 5.46M | i=END_NOT_FOUND; | 64 | 5.46M | }else{ | 65 | 3.28M | got_frame = 1; | 66 | 3.28M | s->state=0; | 67 | 3.28M | i-= s->header_size -1; | 68 | 3.28M | s->remaining_size = len; | 69 | 3.28M | if(!new_frame_start || pc->index+i<=0){ | 70 | 64.0k | s->remaining_size += i; | 71 | 64.0k | goto get_next; | 72 | 64.0k | } | 73 | 3.21M | else if (i < 0) { | 74 | 48.0k | s->remaining_size += i; | 75 | 48.0k | } | 76 | 3.28M | } | 77 | 8.74M | } | 78 | 8.74M | } | 79 | | | 80 | 9.00M | if(ff_combine_frame(pc, i, &buf, &buf_size)<0){ | 81 | 5.72M | s->remaining_size -= FFMIN(s->remaining_size, buf_size); | 82 | 5.72M | *poutbuf = NULL; | 83 | 5.72M | *poutbuf_size = 0; | 84 | 5.72M | return buf_size; | 85 | 5.72M | } | 86 | 9.00M | } | 87 | | | 88 | 3.86M | *poutbuf = buf; | 89 | 3.86M | *poutbuf_size = buf_size; | 90 | | | 91 | 3.86M | if (got_frame) { | 92 | 3.79M | int bit_rate; | 93 | | | 94 | | /* Due to backwards compatible HE-AAC the sample rate, channel count, | 95 | | and total number of samples found in an AAC ADTS header are not | 96 | | reliable. Bit rate is still accurate because the total frame | 97 | | duration in seconds is still correct (as is the number of bits in | 98 | | the frame). */ | 99 | 3.79M | if (avctx->codec_id != AV_CODEC_ID_AAC) { | 100 | 3.79M | #if CONFIG_AC3_PARSER | 101 | 3.79M | AC3HeaderInfo hdr, *phrd = &hdr; | 102 | 3.79M | int offset = ff_ac3_find_syncword(buf, buf_size); | 103 | | | 104 | 3.79M | if (offset < 0) | 105 | 524k | return i; | 106 | | | 107 | 3.27M | buf += offset; | 108 | 3.27M | buf_size -= offset; | 109 | 3.72M | while (buf_size > 0) { | 110 | 3.72M | int ret = avpriv_ac3_parse_header(&phrd, buf, buf_size); | 111 | | | 112 | 3.72M | if (ret < 0 || hdr.frame_size > buf_size) | 113 | 3.04M | return i; | 114 | | | 115 | 682k | if (buf_size > hdr.frame_size) { | 116 | 450k | buf += hdr.frame_size; | 117 | 450k | buf_size -= hdr.frame_size; | 118 | 450k | continue; | 119 | 450k | } | 120 | | /* Check for false positives since the syncword is not enough. | 121 | | See section 6.1.2 of A/52. */ | 122 | 232k | if (av_crc(s->crc_ctx, 0, buf + 2, hdr.frame_size - 2)) | 123 | 184k | return i; | 124 | 47.7k | break; | 125 | 232k | } | 126 | | | 127 | 47.7k | avctx->sample_rate = hdr.sample_rate; | 128 | | | 129 | 47.7k | if (hdr.bitstream_id > 10) | 130 | 41.4k | avctx->codec_id = AV_CODEC_ID_EAC3; | 131 | | | 132 | 47.7k | if (!CONFIG_EAC3_DECODER || avctx->codec_id != AV_CODEC_ID_EAC3) { | 133 | 47.7k | av_channel_layout_uninit(&avctx->ch_layout); | 134 | 47.7k | if (hdr.channel_layout) { | 135 | 47.7k | av_channel_layout_from_mask(&avctx->ch_layout, hdr.channel_layout); | 136 | 47.7k | } else { | 137 | 0 | avctx->ch_layout.order = AV_CHANNEL_ORDER_UNSPEC; | 138 | 0 | avctx->ch_layout.nb_channels = hdr.channels; | 139 | 0 | } | 140 | 47.7k | } | 141 | 47.7k | s1->duration = hdr.num_blocks * 256; | 142 | 47.7k | avctx->audio_service_type = hdr.bitstream_mode; | 143 | 47.7k | if (hdr.bitstream_mode == 0x7 && hdr.channels > 1) | 144 | 2.38k | avctx->audio_service_type = AV_AUDIO_SERVICE_TYPE_KARAOKE; | 145 | 47.7k | bit_rate = hdr.bit_rate; | 146 | 47.7k | #endif | 147 | 47.7k | } else { | 148 | | #if CONFIG_AAC_PARSER | 149 | | AACADTSHeaderInfo hdr; | 150 | | | 151 | | if (buf_size < AV_AAC_ADTS_HEADER_SIZE || | 152 | | ff_adts_header_parse_buf(buf, &hdr) < 0) | 153 | | return i; | 154 | | | 155 | | if (avctx->profile == AV_PROFILE_UNKNOWN) | 156 | | avctx->profile = hdr.object_type - 1; | 157 | | /* ADTS does not support USAC */ | 158 | | s1->key_frame = 1; | 159 | | bit_rate = hdr.bit_rate; | 160 | | #endif | 161 | 0 | } | 162 | | | 163 | | /* Calculate the average bit rate */ | 164 | 47.7k | s->frame_number++; | 165 | 47.7k | if (!CONFIG_EAC3_DECODER || avctx->codec_id != AV_CODEC_ID_EAC3) { | 166 | 47.7k | avctx->bit_rate += | 167 | 47.7k | (bit_rate - avctx->bit_rate) / s->frame_number; | 168 | 47.7k | } | 169 | 47.7k | } | 170 | | | 171 | 116k | return i; | 172 | 3.86M | } |
|