• Main Page
  • Related Pages
  • Modules
  • Data Structures
  • Files
  • Examples
  • File List
  • Globals

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 FFmpeg.
00007  *
00008  * FFmpeg 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  * FFmpeg 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 FFmpeg; 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     switch(channels) {
00067         case 2:
00068             avctx->channel_layout = AV_CH_LAYOUT_STEREO;
00069             break;
00070         case 4:
00071             avctx->channel_layout = AV_CH_LAYOUT_QUAD;
00072             break;
00073         case 8:
00074             avctx->channel_layout = AV_CH_LAYOUT_5POINT1_BACK | AV_CH_LAYOUT_STEREO_DOWNMIX;
00075     }
00076     avctx->sample_rate = 48000;
00077     avctx->bit_rate    = 48000 * avctx->channels * (avctx->bits_per_coded_sample + 4) +
00078                          32 * (48000 / (buf_size * 8 /
00079                                         (avctx->channels *
00080                                          (avctx->bits_per_coded_sample + 4))));
00081 
00082     return frame_size;
00083 }
00084 
00085 static int s302m_decode_frame(AVCodecContext *avctx, void *data,
00086                               int *data_size, AVPacket *avpkt)
00087 {
00088     const uint8_t *buf = avpkt->data;
00089     int buf_size       = avpkt->size;
00090 
00091     int frame_size = s302m_parse_frame_header(avctx, buf, buf_size);
00092     if (frame_size < 0)
00093         return frame_size;
00094 
00095     buf_size -= AES3_HEADER_LEN;
00096     buf      += AES3_HEADER_LEN;
00097 
00098     if (*data_size < 4 * buf_size * 8 / (avctx->bits_per_coded_sample + 4))
00099         return -1;
00100 
00101     if (avctx->bits_per_coded_sample == 24) {
00102         uint32_t *o = data;
00103         for (; buf_size > 6; buf_size -= 7) {
00104             *o++ = (av_reverse[buf[2]]        << 24) |
00105                    (av_reverse[buf[1]]        << 16) |
00106                    (av_reverse[buf[0]]        <<  8);
00107             *o++ = (av_reverse[buf[6] & 0xf0] << 28) |
00108                    (av_reverse[buf[5]]        << 20) |
00109                    (av_reverse[buf[4]]        << 12) |
00110                    (av_reverse[buf[3] & 0x0f] <<  4);
00111             buf += 7;
00112         }
00113         *data_size = (uint8_t*) o - (uint8_t*) data;
00114     } else if (avctx->bits_per_coded_sample == 20) {
00115         uint32_t *o = data;
00116         for (; buf_size > 5; buf_size -= 6) {
00117             *o++ = (av_reverse[buf[2] & 0xf0] << 28) |
00118                    (av_reverse[buf[1]]        << 20) |
00119                    (av_reverse[buf[0]]        << 12);
00120             *o++ = (av_reverse[buf[5] & 0xf0] << 28) |
00121                    (av_reverse[buf[4]]        << 20) |
00122                    (av_reverse[buf[3]]        << 12);
00123             buf += 6;
00124         }
00125         *data_size = (uint8_t*) o - (uint8_t*) data;
00126     } else {
00127         uint16_t *o = data;
00128         for (; buf_size > 4; buf_size -= 5) {
00129             *o++ = (av_reverse[buf[1]]        <<  8) |
00130                     av_reverse[buf[0]];
00131             *o++ = (av_reverse[buf[4] & 0xf0] << 12) |
00132                    (av_reverse[buf[3]]        <<  4) |
00133                    (av_reverse[buf[2]]        >>  4);
00134             buf += 5;
00135         }
00136         *data_size = (uint8_t*) o - (uint8_t*) data;
00137     }
00138 
00139     return buf - avpkt->data;
00140 }
00141 
00142 
00143 AVCodec ff_s302m_decoder = {
00144     .name           = "s302m",
00145     .type           = AVMEDIA_TYPE_AUDIO,
00146     .id             = CODEC_ID_S302M,
00147     .priv_data_size = 0,
00148     .decode         = s302m_decode_frame,
00149     .long_name      = NULL_IF_CONFIG_SMALL("SMPTE 302M"),
00150 };

Generated on Wed Apr 11 2012 07:31:34 for FFmpeg by  doxygen 1.7.1