annotate ffmpeg/libavcodec/latm_parser.c @ 13:844d341cf643 tip

Back up before ISMIR
author Yading Song <yading.song@eecs.qmul.ac.uk>
date Thu, 31 Oct 2013 13:17:06 +0000
parents 6840f77b83aa
children
rev   line source
yading@10 1 /*
yading@10 2 * copyright (c) 2008 Paul Kendall <paul@kcbbs.gen.nz>
yading@10 3 *
yading@10 4 * This file is part of FFmpeg.
yading@10 5 *
yading@10 6 * FFmpeg is free software; you can redistribute it and/or
yading@10 7 * modify it under the terms of the GNU Lesser General Public
yading@10 8 * License as published by the Free Software Foundation; either
yading@10 9 * version 2.1 of the License, or (at your option) any later version.
yading@10 10 *
yading@10 11 * FFmpeg is distributed in the hope that it will be useful,
yading@10 12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
yading@10 13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
yading@10 14 * Lesser General Public License for more details.
yading@10 15 *
yading@10 16 * You should have received a copy of the GNU Lesser General Public
yading@10 17 * License along with FFmpeg; if not, write to the Free Software
yading@10 18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
yading@10 19 */
yading@10 20
yading@10 21 /**
yading@10 22 * @file
yading@10 23 * AAC LATM parser
yading@10 24 */
yading@10 25
yading@10 26 #include <stdint.h>
yading@10 27 #include "parser.h"
yading@10 28
yading@10 29 #define LATM_HEADER 0x56e000 // 0x2b7 (11 bits)
yading@10 30 #define LATM_MASK 0xFFE000 // top 11 bits
yading@10 31 #define LATM_SIZE_MASK 0x001FFF // bottom 13 bits
yading@10 32
yading@10 33 typedef struct LATMParseContext{
yading@10 34 ParseContext pc;
yading@10 35 int count;
yading@10 36 } LATMParseContext;
yading@10 37
yading@10 38 /**
yading@10 39 * Find the end of the current frame in the bitstream.
yading@10 40 * @return the position of the first byte of the next frame, or -1
yading@10 41 */
yading@10 42 static int latm_find_frame_end(AVCodecParserContext *s1, const uint8_t *buf,
yading@10 43 int buf_size)
yading@10 44 {
yading@10 45 LATMParseContext *s = s1->priv_data;
yading@10 46 ParseContext *pc = &s->pc;
yading@10 47 int pic_found, i;
yading@10 48 uint32_t state;
yading@10 49
yading@10 50 pic_found = pc->frame_start_found;
yading@10 51 state = pc->state;
yading@10 52
yading@10 53 if (!pic_found) {
yading@10 54 for (i = 0; i < buf_size; i++) {
yading@10 55 state = (state<<8) | buf[i];
yading@10 56 if ((state & LATM_MASK) == LATM_HEADER) {
yading@10 57 i++;
yading@10 58 s->count = -i;
yading@10 59 pic_found = 1;
yading@10 60 break;
yading@10 61 }
yading@10 62 }
yading@10 63 }
yading@10 64
yading@10 65 if (pic_found) {
yading@10 66 /* EOF considered as end of frame */
yading@10 67 if (buf_size == 0)
yading@10 68 return 0;
yading@10 69 if ((state & LATM_SIZE_MASK) - s->count <= buf_size) {
yading@10 70 pc->frame_start_found = 0;
yading@10 71 pc->state = -1;
yading@10 72 return (state & LATM_SIZE_MASK) - s->count;
yading@10 73 }
yading@10 74 }
yading@10 75
yading@10 76 s->count += buf_size;
yading@10 77 pc->frame_start_found = pic_found;
yading@10 78 pc->state = state;
yading@10 79
yading@10 80 return END_NOT_FOUND;
yading@10 81 }
yading@10 82
yading@10 83 static int latm_parse(AVCodecParserContext *s1, AVCodecContext *avctx,
yading@10 84 const uint8_t **poutbuf, int *poutbuf_size,
yading@10 85 const uint8_t *buf, int buf_size)
yading@10 86 {
yading@10 87 LATMParseContext *s = s1->priv_data;
yading@10 88 ParseContext *pc = &s->pc;
yading@10 89 int next;
yading@10 90
yading@10 91 if (s1->flags & PARSER_FLAG_COMPLETE_FRAMES) {
yading@10 92 next = buf_size;
yading@10 93 } else {
yading@10 94 next = latm_find_frame_end(s1, buf, buf_size);
yading@10 95
yading@10 96 if (ff_combine_frame(pc, next, &buf, &buf_size) < 0) {
yading@10 97 *poutbuf = NULL;
yading@10 98 *poutbuf_size = 0;
yading@10 99 return buf_size;
yading@10 100 }
yading@10 101 }
yading@10 102 *poutbuf = buf;
yading@10 103 *poutbuf_size = buf_size;
yading@10 104 return next;
yading@10 105 }
yading@10 106
yading@10 107 AVCodecParser ff_aac_latm_parser = {
yading@10 108 .codec_ids = { AV_CODEC_ID_AAC_LATM },
yading@10 109 .priv_data_size = sizeof(LATMParseContext),
yading@10 110 .parser_parse = latm_parse,
yading@10 111 .parser_close = ff_parse_close
yading@10 112 };