Libav 0.7.1
libavcodec/s302m.c
Go to the documentation of this file.
00001 /*
00002  * SMPTE 302M decoder
00003  * Copyright (c) 2008 Laurent Aimar <fenrir@videolan.org>
00004  * Copyright (c) 2009 Baptiste Coudurier <baptiste.coudurier@gmail.com>
00005  *
00006  * This file is part of Libav.
00007  *
00008  * Libav is free software; you can redistribute it and/or
00009  * modify it under the terms of the GNU Lesser General Public
00010  * License as published by the Free Software Foundation; either
00011  * version 2.1 of the License, or (at your option) any later version.
00012  *
00013  * Libav is distributed in the hope that it will be useful,
00014  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00015  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00016  * Lesser General Public License for more details.
00017  *
00018  * You should have received a copy of the GNU Lesser General Public
00019  * License along with Libav; if not, write to the Free Software
00020  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
00021  */
00022 
00023 #include "libavutil/intreadwrite.h"
00024 #include "avcodec.h"
00025 
00026 #define AES3_HEADER_LEN 4
00027 
00028 static int s302m_parse_frame_header(AVCodecContext *avctx, const uint8_t *buf,
00029                                     int buf_size)
00030 {
00031     uint32_t h;
00032     int frame_size, channels, bits;
00033 
00034     if (buf_size <= AES3_HEADER_LEN) {
00035         av_log(avctx, AV_LOG_ERROR, "frame is too short\n");
00036         return AVERROR_INVALIDDATA;
00037     }
00038 
00039     /*
00040      * AES3 header :
00041      * size:            16
00042      * number channels   2
00043      * channel_id        8
00044      * bits per samples  2
00045      * alignments        4
00046      */
00047 
00048     h = AV_RB32(buf);
00049     frame_size =  (h >> 16) & 0xffff;
00050     channels   = ((h >> 14) & 0x0003) * 2 +  2;
00051     bits       = ((h >>  4) & 0x0003) * 4 + 16;
00052 
00053     if (AES3_HEADER_LEN + frame_size != buf_size || bits > 24) {
00054         av_log(avctx, AV_LOG_ERROR, "frame has invalid header\n");
00055         return AVERROR_INVALIDDATA;
00056     }
00057 
00058     /* Set output properties */
00059     avctx->bits_per_coded_sample = bits;
00060     if (bits > 16)
00061         avctx->sample_fmt = SAMPLE_FMT_S32;
00062     else
00063         avctx->sample_fmt = SAMPLE_FMT_S16;
00064 
00065     avctx->channels    = channels;
00066     avctx->sample_rate = 48000;
00067     avctx->bit_rate    = 48000 * avctx->channels * (avctx->bits_per_coded_sample + 4) +
00068                          32 * (48000 / (buf_size * 8 /
00069                                         (avctx->channels *
00070                                          (avctx->bits_per_coded_sample + 4))));
00071 
00072     return frame_size;
00073 }
00074 
00075 static int s302m_decode_frame(AVCodecContext *avctx, void *data,
00076                               int *data_size, AVPacket *avpkt)
00077 {
00078     const uint8_t *buf = avpkt->data;
00079     int buf_size       = avpkt->size;
00080 
00081     int frame_size = s302m_parse_frame_header(avctx, buf, buf_size);
00082     if (frame_size < 0)
00083         return frame_size;
00084 
00085     buf_size -= AES3_HEADER_LEN;
00086     buf      += AES3_HEADER_LEN;
00087 
00088     if (*data_size < 4 * buf_size * 8 / (avctx->bits_per_coded_sample + 4))
00089         return -1;
00090 
00091     if (avctx->bits_per_coded_sample == 24) {
00092         uint32_t *o = data;
00093         for (; buf_size > 6; buf_size -= 7) {
00094             *o++ = (av_reverse[buf[2]]        << 24) |
00095                    (av_reverse[buf[1]]        << 16) |
00096                    (av_reverse[buf[0]]        <<  8);
00097             *o++ = (av_reverse[buf[6] & 0xf0] << 28) |
00098                    (av_reverse[buf[5]]        << 20) |
00099                    (av_reverse[buf[4]]        << 12) |
00100                    (av_reverse[buf[3] & 0x0f] <<  4);
00101             buf += 7;
00102         }
00103         *data_size = (uint8_t*) o - (uint8_t*) data;
00104     } else if (avctx->bits_per_coded_sample == 20) {
00105         uint32_t *o = data;
00106         for (; buf_size > 5; buf_size -= 6) {
00107             *o++ = (av_reverse[buf[2] & 0xf0] << 28) |
00108                    (av_reverse[buf[1]]        << 20) |
00109                    (av_reverse[buf[0]]        << 12);
00110             *o++ = (av_reverse[buf[5] & 0xf0] << 28) |
00111                    (av_reverse[buf[4]]        << 20) |
00112                    (av_reverse[buf[3]]        << 12);
00113             buf += 6;
00114         }
00115         *data_size = (uint8_t*) o - (uint8_t*) data;
00116     } else {
00117         uint16_t *o = data;
00118         for (; buf_size > 4; buf_size -= 5) {
00119             *o++ = (av_reverse[buf[1]]        <<  8) |
00120                     av_reverse[buf[0]];
00121             *o++ = (av_reverse[buf[4] & 0xf0] << 12) |
00122                    (av_reverse[buf[3]]        <<  4) |
00123                    (av_reverse[buf[2]]        >>  4);
00124             buf += 5;
00125         }
00126         *data_size = (uint8_t*) o - (uint8_t*) data;
00127     }
00128 
00129     return buf - avpkt->data;
00130 }
00131 
00132 
00133 AVCodec ff_s302m_decoder = {
00134     .name           = "s302m",
00135     .type           = AVMEDIA_TYPE_AUDIO,
00136     .id             = CODEC_ID_S302M,
00137     .priv_data_size = 0,
00138     .decode         = s302m_decode_frame,
00139     .long_name      = NULL_IF_CONFIG_SMALL("SMPTE 302M"),
00140 };