vf_*_vaapi: Support increasing hardware frame pool size
[libav.git] / libavcodec / mpegaudiodecheader.c
CommitLineData
4bd8e17c
AJ
1/*
2 * MPEG Audio header decoder
406792e7 3 * Copyright (c) 2001, 2002 Fabrice Bellard
4bd8e17c 4 *
2912e87a 5 * This file is part of Libav.
4bd8e17c 6 *
2912e87a 7 * Libav is free software; you can redistribute it and/or
4bd8e17c
AJ
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 *
2912e87a 12 * Libav is distributed in the hope that it will be useful,
4bd8e17c
AJ
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
2912e87a 18 * License along with Libav; if not, write to the Free Software
4bd8e17c
AJ
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20 */
21
22/**
ba87f080 23 * @file
4bd8e17c
AJ
24 * MPEG Audio header decoder.
25 */
26
44127546
JR
27#include "libavutil/common.h"
28
4bd8e17c 29#include "avcodec.h"
58170b02 30#include "internal.h"
4bd8e17c
AJ
31#include "mpegaudio.h"
32#include "mpegaudiodata.h"
c6e1599b 33#include "mpegaudiodecheader.h"
4bd8e17c
AJ
34
35
82ab61f9 36int avpriv_mpegaudio_decode_header(MPADecodeHeader *s, uint32_t header)
4bd8e17c
AJ
37{
38 int sample_rate, frame_size, mpeg25, padding;
39 int sample_rate_index, bitrate_index;
955aec3c
AK
40 int ret;
41
42 ret = ff_mpa_check_header(header);
43 if (ret < 0)
44 return ret;
45
4bd8e17c
AJ
46 if (header & (1<<20)) {
47 s->lsf = (header & (1<<19)) ? 0 : 1;
48 mpeg25 = 0;
49 } else {
50 s->lsf = 1;
51 mpeg25 = 1;
52 }
53
54 s->layer = 4 - ((header >> 17) & 3);
55 /* extract frequency */
56 sample_rate_index = (header >> 10) & 3;
44127546
JR
57 if (sample_rate_index >= FF_ARRAY_ELEMS(avpriv_mpa_freq_tab))
58 sample_rate_index = 0;
82ab61f9 59 sample_rate = avpriv_mpa_freq_tab[sample_rate_index] >> (s->lsf + mpeg25);
4bd8e17c
AJ
60 sample_rate_index += 3 * (s->lsf + mpeg25);
61 s->sample_rate_index = sample_rate_index;
62 s->error_protection = ((header >> 16) & 1) ^ 1;
63 s->sample_rate = sample_rate;
64
65 bitrate_index = (header >> 12) & 0xf;
66 padding = (header >> 9) & 1;
67 //extension = (header >> 8) & 1;
68 s->mode = (header >> 6) & 3;
69 s->mode_ext = (header >> 4) & 3;
70 //copyright = (header >> 3) & 1;
71 //original = (header >> 2) & 1;
72 //emphasis = header & 3;
73
74 if (s->mode == MPA_MONO)
75 s->nb_channels = 1;
76 else
77 s->nb_channels = 2;
78
79 if (bitrate_index != 0) {
82ab61f9 80 frame_size = avpriv_mpa_bitrate_tab[s->lsf][s->layer - 1][bitrate_index];
4bd8e17c
AJ
81 s->bit_rate = frame_size * 1000;
82 switch(s->layer) {
83 case 1:
84 frame_size = (frame_size * 12000) / sample_rate;
85 frame_size = (frame_size + padding) * 4;
86 break;
87 case 2:
88 frame_size = (frame_size * 144000) / sample_rate;
89 frame_size += padding;
90 break;
91 default:
92 case 3:
93 frame_size = (frame_size * 144000) / (sample_rate << s->lsf);
94 frame_size += padding;
95 break;
96 }
97 s->frame_size = frame_size;
98 } else {
99 /* if no frame size computed, signal it */
100 return 1;
101 }
102
103#if defined(DEBUG)
6a85dfc8 104 ff_dlog(NULL, "layer%d, %d Hz, %d kbits/s, ",
4bd8e17c
AJ
105 s->layer, s->sample_rate, s->bit_rate);
106 if (s->nb_channels == 2) {
107 if (s->layer == 3) {
108 if (s->mode_ext & MODE_EXT_MS_STEREO)
6a85dfc8 109 ff_dlog(NULL, "ms-");
4bd8e17c 110 if (s->mode_ext & MODE_EXT_I_STEREO)
6a85dfc8 111 ff_dlog(NULL, "i-");
4bd8e17c 112 }
6a85dfc8 113 ff_dlog(NULL, "stereo");
4bd8e17c 114 } else {
6a85dfc8 115 ff_dlog(NULL, "mono");
4bd8e17c 116 }
6a85dfc8 117 ff_dlog(NULL, "\n");
4bd8e17c
AJ
118#endif
119 return 0;
120}
0199e00b 121
de9e199a
AK
122int ff_mpa_decode_header(AVCodecContext *avctx, uint32_t head, int *sample_rate,
123 int *channels, int *frame_size, int *bit_rate)
0199e00b
MR
124{
125 MPADecodeHeader s1, *s = &s1;
126
82ab61f9 127 if (avpriv_mpegaudio_decode_header(s, head) != 0) {
0199e00b
MR
128 return -1;
129 }
130
131 switch(s->layer) {
132 case 1:
36ef5369 133 avctx->codec_id = AV_CODEC_ID_MP1;
0199e00b
MR
134 *frame_size = 384;
135 break;
136 case 2:
36ef5369 137 avctx->codec_id = AV_CODEC_ID_MP2;
0199e00b
MR
138 *frame_size = 1152;
139 break;
140 default:
141 case 3:
5cbae565
GC
142 if (avctx->codec_id != AV_CODEC_ID_MP3ADU)
143 avctx->codec_id = AV_CODEC_ID_MP3;
0199e00b
MR
144 if (s->lsf)
145 *frame_size = 576;
146 else
147 *frame_size = 1152;
148 break;
149 }
150
151 *sample_rate = s->sample_rate;
152 *channels = s->nb_channels;
153 *bit_rate = s->bit_rate;
0199e00b
MR
154 return s->frame_size;
155}
de9e199a
AK
156
157#if LIBAVCODEC_VERSION_MAJOR < 57
158int avpriv_mpa_decode_header(AVCodecContext *avctx, uint32_t head, int *sample_rate, int *channels, int *frame_size, int *bit_rate)
159{
160 return ff_mpa_decode_header(avctx, head, sample_rate, channels, frame_size, bit_rate);
161}
162#endif