Libav 0.7.1
libavcodec/latm_parser.c
Go to the documentation of this file.
00001 /*
00002  * copyright (c) 2008 Paul Kendall <paul@kcbbs.gen.nz>
00003  *
00004  * This file is part of Libav.
00005  *
00006  * Libav is free software; you can redistribute it and/or
00007  * modify it under the terms of the GNU Lesser General Public
00008  * License as published by the Free Software Foundation; either
00009  * version 2.1 of the License, or (at your option) any later version.
00010  *
00011  * Libav is distributed in the hope that it will be useful,
00012  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00013  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00014  * Lesser General Public License for more details.
00015  *
00016  * You should have received a copy of the GNU Lesser General Public
00017  * License along with Libav; if not, write to the Free Software
00018  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
00019  */
00020 
00026 #include <stdint.h>
00027 #include "parser.h"
00028 
00029 #define LATM_HEADER     0x56e000        // 0x2b7 (11 bits)
00030 #define LATM_MASK       0xFFE000        // top 11 bits
00031 #define LATM_SIZE_MASK  0x001FFF        // bottom 13 bits
00032 
00033 typedef struct LATMParseContext{
00034     ParseContext pc;
00035     int count;
00036 } LATMParseContext;
00037 
00042 static int latm_find_frame_end(AVCodecParserContext *s1, const uint8_t *buf,
00043                                int buf_size)
00044 {
00045     LATMParseContext *s = s1->priv_data;
00046     ParseContext *pc    = &s->pc;
00047     int pic_found, i;
00048     uint32_t state;
00049 
00050     pic_found = pc->frame_start_found;
00051     state     = pc->state;
00052 
00053     i = 0;
00054     if (!pic_found) {
00055         for (i = 0; i < buf_size; i++) {
00056             state = (state<<8) | buf[i];
00057             if ((state & LATM_MASK) == LATM_HEADER) {
00058                 i++;
00059                 s->count  = -i;
00060                 pic_found = 1;
00061                 break;
00062             }
00063         }
00064     }
00065 
00066     if (pic_found) {
00067         /* EOF considered as end of frame */
00068         if (buf_size == 0)
00069             return 0;
00070         if ((state & LATM_SIZE_MASK) - s->count <= buf_size) {
00071             pc->frame_start_found = 0;
00072             pc->state             = -1;
00073             return (state & LATM_SIZE_MASK) - s->count;
00074         }
00075     }
00076 
00077     s->count             += buf_size;
00078     pc->frame_start_found = pic_found;
00079     pc->state             = state;
00080 
00081     return END_NOT_FOUND;
00082 }
00083 
00084 static int latm_parse(AVCodecParserContext *s1, AVCodecContext *avctx,
00085                       const uint8_t **poutbuf, int *poutbuf_size,
00086                       const uint8_t *buf, int buf_size)
00087 {
00088     LATMParseContext *s = s1->priv_data;
00089     ParseContext *pc    = &s->pc;
00090     int next;
00091 
00092     if (s1->flags & PARSER_FLAG_COMPLETE_FRAMES) {
00093         next = buf_size;
00094     } else {
00095         next = latm_find_frame_end(s1, buf, buf_size);
00096 
00097         if (ff_combine_frame(pc, next, &buf, &buf_size) < 0) {
00098             *poutbuf      = NULL;
00099             *poutbuf_size = 0;
00100             return buf_size;
00101         }
00102     }
00103     *poutbuf      = buf;
00104     *poutbuf_size = buf_size;
00105     return next;
00106 }
00107 
00108 AVCodecParser ff_aac_latm_parser = {
00109     { CODEC_ID_AAC_LATM },
00110     sizeof(LATMParseContext),
00111     NULL,
00112     latm_parse,
00113     ff_parse_close
00114 };