yading@10: /* yading@10: * copyright (c) 2008 Paul Kendall yading@10: * yading@10: * This file is part of FFmpeg. yading@10: * yading@10: * FFmpeg is free software; you can redistribute it and/or yading@10: * modify it under the terms of the GNU Lesser General Public yading@10: * License as published by the Free Software Foundation; either yading@10: * version 2.1 of the License, or (at your option) any later version. yading@10: * yading@10: * FFmpeg is distributed in the hope that it will be useful, yading@10: * but WITHOUT ANY WARRANTY; without even the implied warranty of yading@10: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU yading@10: * Lesser General Public License for more details. yading@10: * yading@10: * You should have received a copy of the GNU Lesser General Public yading@10: * License along with FFmpeg; if not, write to the Free Software yading@10: * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA yading@10: */ yading@10: yading@10: /** yading@10: * @file yading@10: * AAC LATM parser yading@10: */ yading@10: yading@10: #include yading@10: #include "parser.h" yading@10: yading@10: #define LATM_HEADER 0x56e000 // 0x2b7 (11 bits) yading@10: #define LATM_MASK 0xFFE000 // top 11 bits yading@10: #define LATM_SIZE_MASK 0x001FFF // bottom 13 bits yading@10: yading@10: typedef struct LATMParseContext{ yading@10: ParseContext pc; yading@10: int count; yading@10: } LATMParseContext; yading@10: yading@10: /** yading@10: * Find the end of the current frame in the bitstream. yading@10: * @return the position of the first byte of the next frame, or -1 yading@10: */ yading@10: static int latm_find_frame_end(AVCodecParserContext *s1, const uint8_t *buf, yading@10: int buf_size) yading@10: { yading@10: LATMParseContext *s = s1->priv_data; yading@10: ParseContext *pc = &s->pc; yading@10: int pic_found, i; yading@10: uint32_t state; yading@10: yading@10: pic_found = pc->frame_start_found; yading@10: state = pc->state; yading@10: yading@10: if (!pic_found) { yading@10: for (i = 0; i < buf_size; i++) { yading@10: state = (state<<8) | buf[i]; yading@10: if ((state & LATM_MASK) == LATM_HEADER) { yading@10: i++; yading@10: s->count = -i; yading@10: pic_found = 1; yading@10: break; yading@10: } yading@10: } yading@10: } yading@10: yading@10: if (pic_found) { yading@10: /* EOF considered as end of frame */ yading@10: if (buf_size == 0) yading@10: return 0; yading@10: if ((state & LATM_SIZE_MASK) - s->count <= buf_size) { yading@10: pc->frame_start_found = 0; yading@10: pc->state = -1; yading@10: return (state & LATM_SIZE_MASK) - s->count; yading@10: } yading@10: } yading@10: yading@10: s->count += buf_size; yading@10: pc->frame_start_found = pic_found; yading@10: pc->state = state; yading@10: yading@10: return END_NOT_FOUND; yading@10: } yading@10: yading@10: static int latm_parse(AVCodecParserContext *s1, AVCodecContext *avctx, yading@10: const uint8_t **poutbuf, int *poutbuf_size, yading@10: const uint8_t *buf, int buf_size) yading@10: { yading@10: LATMParseContext *s = s1->priv_data; yading@10: ParseContext *pc = &s->pc; yading@10: int next; yading@10: yading@10: if (s1->flags & PARSER_FLAG_COMPLETE_FRAMES) { yading@10: next = buf_size; yading@10: } else { yading@10: next = latm_find_frame_end(s1, buf, buf_size); yading@10: yading@10: if (ff_combine_frame(pc, next, &buf, &buf_size) < 0) { yading@10: *poutbuf = NULL; yading@10: *poutbuf_size = 0; yading@10: return buf_size; yading@10: } yading@10: } yading@10: *poutbuf = buf; yading@10: *poutbuf_size = buf_size; yading@10: return next; yading@10: } yading@10: yading@10: AVCodecParser ff_aac_latm_parser = { yading@10: .codec_ids = { AV_CODEC_ID_AAC_LATM }, yading@10: .priv_data_size = sizeof(LATMParseContext), yading@10: .parser_parse = latm_parse, yading@10: .parser_close = ff_parse_close yading@10: };