]> git.sesse.net Git - ffmpeg/blob - libavcodec/s302m.c
avcodec/jpeg2000dwt: Fill array using 2d instead of 1d accesses
[ffmpeg] / libavcodec / s302m.c
1 /*
2  * SMPTE 302M decoder
3  * Copyright (c) 2008 Laurent Aimar <fenrir@videolan.org>
4  * Copyright (c) 2009 Baptiste Coudurier <baptiste.coudurier@gmail.com>
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 "libavutil/intreadwrite.h"
24 #include "libavutil/opt.h"
25 #include "libavutil/log.h"
26 #include "avcodec.h"
27 #include "internal.h"
28 #include "mathops.h"
29
30 #define AES3_HEADER_LEN 4
31
32 typedef struct S302Context {
33     AVClass *class;
34     int non_pcm_mode;
35 } S302Context;
36
37 static int s302m_parse_frame_header(AVCodecContext *avctx, const uint8_t *buf,
38                                     int buf_size)
39 {
40     uint32_t h;
41     int frame_size, channels, bits;
42
43     if (buf_size <= AES3_HEADER_LEN) {
44         av_log(avctx, AV_LOG_ERROR, "frame is too short\n");
45         return AVERROR_INVALIDDATA;
46     }
47
48     /*
49      * AES3 header :
50      * size:            16
51      * number channels   2
52      * channel_id        8
53      * bits per samples  2
54      * alignments        4
55      */
56
57     h = AV_RB32(buf);
58     frame_size =  (h >> 16) & 0xffff;
59     channels   = ((h >> 14) & 0x0003) * 2 +  2;
60     bits       = ((h >>  4) & 0x0003) * 4 + 16;
61
62     if (AES3_HEADER_LEN + frame_size != buf_size || bits > 24) {
63         av_log(avctx, AV_LOG_ERROR, "frame has invalid header\n");
64         return AVERROR_INVALIDDATA;
65     }
66
67     /* Set output properties */
68     avctx->bits_per_raw_sample = bits;
69     if (bits > 16)
70         avctx->sample_fmt = AV_SAMPLE_FMT_S32;
71     else
72         avctx->sample_fmt = AV_SAMPLE_FMT_S16;
73
74     avctx->channels    = channels;
75     switch(channels) {
76         case 2:
77             avctx->channel_layout = AV_CH_LAYOUT_STEREO;
78             break;
79         case 4:
80             avctx->channel_layout = AV_CH_LAYOUT_QUAD;
81             break;
82         case 6:
83             avctx->channel_layout = AV_CH_LAYOUT_5POINT1_BACK;
84             break;
85         case 8:
86             avctx->channel_layout = AV_CH_LAYOUT_5POINT1_BACK | AV_CH_LAYOUT_STEREO_DOWNMIX;
87     }
88     avctx->bit_rate    = 48000 * avctx->channels * (avctx->bits_per_raw_sample + 4) +
89                          32 * (48000 / (buf_size * 8 /
90                                         (avctx->channels *
91                                          (avctx->bits_per_raw_sample + 4))));
92
93     return frame_size;
94 }
95
96 static int s302m_decode_frame(AVCodecContext *avctx, void *data,
97                               int *got_frame_ptr, AVPacket *avpkt)
98 {
99     S302Context *s = avctx->priv_data;
100     AVFrame *frame     = data;
101     const uint8_t *buf = avpkt->data;
102     int buf_size       = avpkt->size;
103     int block_size, ret;
104     int i;
105     int non_pcm_data_type = -1;
106
107     int frame_size = s302m_parse_frame_header(avctx, buf, buf_size);
108     if (frame_size < 0)
109         return frame_size;
110
111     buf_size -= AES3_HEADER_LEN;
112     buf      += AES3_HEADER_LEN;
113
114     /* get output buffer */
115     block_size = (avctx->bits_per_raw_sample + 4) / 4;
116     frame->nb_samples = 2 * (buf_size / block_size) / avctx->channels;
117     if ((ret = ff_get_buffer(avctx, frame, 0)) < 0)
118         return ret;
119
120     buf_size = (frame->nb_samples * avctx->channels / 2) * block_size;
121
122     if (avctx->bits_per_raw_sample == 24) {
123         uint32_t *o = (uint32_t *)frame->data[0];
124         for (; buf_size > 6; buf_size -= 7) {
125             *o++ = (ff_reverse[buf[2]]        << 24) |
126                    (ff_reverse[buf[1]]        << 16) |
127                    (ff_reverse[buf[0]]        <<  8);
128             *o++ = (ff_reverse[buf[6] & 0xf0] << 28) |
129                    (ff_reverse[buf[5]]        << 20) |
130                    (ff_reverse[buf[4]]        << 12) |
131                    (ff_reverse[buf[3] & 0x0f] <<  4);
132             buf += 7;
133         }
134         o = (uint32_t *)frame->data[0];
135         if (avctx->channels == 2)
136             for (i=0; i<frame->nb_samples * 2 - 6; i+=2) {
137                 if (o[i] || o[i+1] || o[i+2] || o[i+3])
138                     break;
139                 if (o[i+4] == 0x96F87200U && o[i+5] == 0xA54E1F00) {
140                     non_pcm_data_type = (o[i+6] >> 16) & 0x1F;
141                     break;
142                 }
143             }
144     } else if (avctx->bits_per_raw_sample == 20) {
145         uint32_t *o = (uint32_t *)frame->data[0];
146         for (; buf_size > 5; buf_size -= 6) {
147             *o++ = (ff_reverse[buf[2] & 0xf0] << 28) |
148                    (ff_reverse[buf[1]]        << 20) |
149                    (ff_reverse[buf[0]]        << 12);
150             *o++ = (ff_reverse[buf[5] & 0xf0] << 28) |
151                    (ff_reverse[buf[4]]        << 20) |
152                    (ff_reverse[buf[3]]        << 12);
153             buf += 6;
154         }
155         o = (uint32_t *)frame->data[0];
156         if (avctx->channels == 2)
157             for (i=0; i<frame->nb_samples * 2 - 6; i+=2) {
158                 if (o[i] || o[i+1] || o[i+2] || o[i+3])
159                     break;
160                 if (o[i+4] == 0x6F872000U && o[i+5] == 0x54E1F000) {
161                     non_pcm_data_type = (o[i+6] >> 16) & 0x1F;
162                     break;
163                 }
164             }
165     } else {
166         uint16_t *o = (uint16_t *)frame->data[0];
167         for (; buf_size > 4; buf_size -= 5) {
168             *o++ = (ff_reverse[buf[1]]        <<  8) |
169                     ff_reverse[buf[0]];
170             *o++ = (ff_reverse[buf[4] & 0xf0] << 12) |
171                    (ff_reverse[buf[3]]        <<  4) |
172                    (ff_reverse[buf[2]]        >>  4);
173             buf += 5;
174         }
175         o = (uint16_t *)frame->data[0];
176         if (avctx->channels == 2)
177             for (i=0; i<frame->nb_samples * 2 - 6; i+=2) {
178                 if (o[i] || o[i+1] || o[i+2] || o[i+3])
179                     break;
180                 if (o[i+4] == 0xF872U && o[i+5] == 0x4E1F) {
181                     non_pcm_data_type = (o[i+6] & 0x1F);
182                     break;
183                 }
184             }
185     }
186
187     if (non_pcm_data_type != -1) {
188         if (s->non_pcm_mode == 3) {
189             av_log(avctx, AV_LOG_ERROR,
190                    "S302 non PCM mode with data type %d not supported\n",
191                    non_pcm_data_type);
192             return AVERROR_PATCHWELCOME;
193         }
194         if (s->non_pcm_mode & 1) {
195             return avpkt->size;
196         }
197     }
198
199     avctx->sample_rate = 48000;
200
201     *got_frame_ptr = 1;
202
203     return avpkt->size;
204 }
205
206 #define FLAGS AV_OPT_FLAG_VIDEO_PARAM|AV_OPT_FLAG_DECODING_PARAM
207 static const AVOption s302m_options[] = {
208     {"non_pcm_mode", "Chooses what to do with NON-PCM", offsetof(S302Context, non_pcm_mode), FF_OPT_TYPE_INT, {.i64 = 3}, 0, 3, FLAGS, "non_pcm_mode"},
209     {"copy"        , "Pass NON-PCM through unchanged"     , 0, FF_OPT_TYPE_CONST, {.i64 = 0}, 0, 3, FLAGS, "non_pcm_mode"},
210     {"drop"        , "Drop NON-PCM"                       , 0, FF_OPT_TYPE_CONST, {.i64 = 1}, 0, 3, FLAGS, "non_pcm_mode"},
211     {"decode_copy" , "Decode if possible else passthrough", 0, FF_OPT_TYPE_CONST, {.i64 = 2}, 0, 3, FLAGS, "non_pcm_mode"},
212     {"decode_drop" , "Decode if possible else drop"       , 0, FF_OPT_TYPE_CONST, {.i64 = 3}, 0, 3, FLAGS, "non_pcm_mode"},
213     {NULL}
214 };
215
216 static const AVClass s302m_class = {
217     "SMPTE 302M Decoder",
218     av_default_item_name,
219     s302m_options,
220     LIBAVUTIL_VERSION_INT,
221 };
222
223 AVCodec ff_s302m_decoder = {
224     .name           = "s302m",
225     .long_name      = NULL_IF_CONFIG_SMALL("SMPTE 302M"),
226     .type           = AVMEDIA_TYPE_AUDIO,
227     .id             = AV_CODEC_ID_S302M,
228     .priv_data_size = sizeof(S302Context),
229     .decode         = s302m_decode_frame,
230     .capabilities   = CODEC_CAP_DR1,
231     .priv_class     = &s302m_class,
232 };