• 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 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 typedef struct S302MDecodeContext {
00029     AVFrame frame;
00030 } S302MDecodeContext;
00031 
00032 static int s302m_parse_frame_header(AVCodecContext *avctx, const uint8_t *buf,
00033                                     int buf_size)
00034 {
00035     uint32_t h;
00036     int frame_size, channels, bits;
00037 
00038     if (buf_size <= AES3_HEADER_LEN) {
00039         av_log(avctx, AV_LOG_ERROR, "frame is too short\n");
00040         return AVERROR_INVALIDDATA;
00041     }
00042 
00043     /*
00044      * AES3 header :
00045      * size:            16
00046      * number channels   2
00047      * channel_id        8
00048      * bits per samples  2
00049      * alignments        4
00050      */
00051 
00052     h = AV_RB32(buf);
00053     frame_size =  (h >> 16) & 0xffff;
00054     channels   = ((h >> 14) & 0x0003) * 2 +  2;
00055     bits       = ((h >>  4) & 0x0003) * 4 + 16;
00056 
00057     if (AES3_HEADER_LEN + frame_size != buf_size || bits > 24) {
00058         av_log(avctx, AV_LOG_ERROR, "frame has invalid header\n");
00059         return AVERROR_INVALIDDATA;
00060     }
00061 
00062     /* Set output properties */
00063     avctx->bits_per_coded_sample = bits;
00064     if (bits > 16)
00065         avctx->sample_fmt = AV_SAMPLE_FMT_S32;
00066     else
00067         avctx->sample_fmt = AV_SAMPLE_FMT_S16;
00068 
00069     avctx->channels    = channels;
00070     avctx->sample_rate = 48000;
00071     avctx->bit_rate    = 48000 * avctx->channels * (avctx->bits_per_coded_sample + 4) +
00072                          32 * (48000 / (buf_size * 8 /
00073                                         (avctx->channels *
00074                                          (avctx->bits_per_coded_sample + 4))));
00075 
00076     return frame_size;
00077 }
00078 
00079 static int s302m_decode_frame(AVCodecContext *avctx, void *data,
00080                               int *got_frame_ptr, AVPacket *avpkt)
00081 {
00082     S302MDecodeContext *s = avctx->priv_data;
00083     const uint8_t *buf = avpkt->data;
00084     int buf_size       = avpkt->size;
00085     int block_size, ret;
00086 
00087     int frame_size = s302m_parse_frame_header(avctx, buf, buf_size);
00088     if (frame_size < 0)
00089         return frame_size;
00090 
00091     buf_size -= AES3_HEADER_LEN;
00092     buf      += AES3_HEADER_LEN;
00093 
00094     /* get output buffer */
00095     block_size = (avctx->bits_per_coded_sample + 4) / 4;
00096     s->frame.nb_samples = 2 * (buf_size / block_size) / avctx->channels;
00097     if ((ret = avctx->get_buffer(avctx, &s->frame)) < 0) {
00098         av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
00099         return ret;
00100     }
00101 
00102     buf_size = (s->frame.nb_samples * avctx->channels / 2) * block_size;
00103 
00104     if (avctx->bits_per_coded_sample == 24) {
00105         uint32_t *o = (uint32_t *)s->frame.data[0];
00106         for (; buf_size > 6; buf_size -= 7) {
00107             *o++ = (av_reverse[buf[2]]        << 24) |
00108                    (av_reverse[buf[1]]        << 16) |
00109                    (av_reverse[buf[0]]        <<  8);
00110             *o++ = (av_reverse[buf[6] & 0xf0] << 28) |
00111                    (av_reverse[buf[5]]        << 20) |
00112                    (av_reverse[buf[4]]        << 12) |
00113                    (av_reverse[buf[3] & 0x0f] <<  4);
00114             buf += 7;
00115         }
00116     } else if (avctx->bits_per_coded_sample == 20) {
00117         uint32_t *o = (uint32_t *)s->frame.data[0];
00118         for (; buf_size > 5; buf_size -= 6) {
00119             *o++ = (av_reverse[buf[2] & 0xf0] << 28) |
00120                    (av_reverse[buf[1]]        << 20) |
00121                    (av_reverse[buf[0]]        << 12);
00122             *o++ = (av_reverse[buf[5] & 0xf0] << 28) |
00123                    (av_reverse[buf[4]]        << 20) |
00124                    (av_reverse[buf[3]]        << 12);
00125             buf += 6;
00126         }
00127     } else {
00128         uint16_t *o = (uint16_t *)s->frame.data[0];
00129         for (; buf_size > 4; buf_size -= 5) {
00130             *o++ = (av_reverse[buf[1]]        <<  8) |
00131                     av_reverse[buf[0]];
00132             *o++ = (av_reverse[buf[4] & 0xf0] << 12) |
00133                    (av_reverse[buf[3]]        <<  4) |
00134                    (av_reverse[buf[2]]        >>  4);
00135             buf += 5;
00136         }
00137     }
00138 
00139     *got_frame_ptr   = 1;
00140     *(AVFrame *)data = s->frame;
00141 
00142     return avpkt->size;
00143 }
00144 
00145 static int s302m_decode_init(AVCodecContext *avctx)
00146 {
00147     S302MDecodeContext *s = avctx->priv_data;
00148 
00149     avcodec_get_frame_defaults(&s->frame);
00150     avctx->coded_frame = &s->frame;
00151 
00152     return 0;
00153 }
00154 
00155 
00156 AVCodec ff_s302m_decoder = {
00157     .name           = "s302m",
00158     .type           = AVMEDIA_TYPE_AUDIO,
00159     .id             = CODEC_ID_S302M,
00160     .priv_data_size = sizeof(S302MDecodeContext),
00161     .init           = s302m_decode_init,
00162     .decode         = s302m_decode_frame,
00163     .capabilities   = CODEC_CAP_DR1,
00164     .long_name      = NULL_IF_CONFIG_SMALL("SMPTE 302M"),
00165 };
Generated on Sun Apr 22 2012 21:54:04 for Libav by doxygen 1.7.1