00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
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 if (!pic_found) {
00054 for (i = 0; i < buf_size; i++) {
00055 state = (state<<8) | buf[i];
00056 if ((state & LATM_MASK) == LATM_HEADER) {
00057 i++;
00058 s->count = -i;
00059 pic_found = 1;
00060 break;
00061 }
00062 }
00063 }
00064
00065 if (pic_found) {
00066
00067 if (buf_size == 0)
00068 return 0;
00069 if ((state & LATM_SIZE_MASK) - s->count <= buf_size) {
00070 pc->frame_start_found = 0;
00071 pc->state = -1;
00072 return (state & LATM_SIZE_MASK) - s->count;
00073 }
00074 }
00075
00076 s->count += buf_size;
00077 pc->frame_start_found = pic_found;
00078 pc->state = state;
00079
00080 return END_NOT_FOUND;
00081 }
00082
00083 static int latm_parse(AVCodecParserContext *s1, AVCodecContext *avctx,
00084 const uint8_t **poutbuf, int *poutbuf_size,
00085 const uint8_t *buf, int buf_size)
00086 {
00087 LATMParseContext *s = s1->priv_data;
00088 ParseContext *pc = &s->pc;
00089 int next;
00090
00091 if (s1->flags & PARSER_FLAG_COMPLETE_FRAMES) {
00092 next = buf_size;
00093 } else {
00094 next = latm_find_frame_end(s1, buf, buf_size);
00095
00096 if (ff_combine_frame(pc, next, &buf, &buf_size) < 0) {
00097 *poutbuf = NULL;
00098 *poutbuf_size = 0;
00099 return buf_size;
00100 }
00101 }
00102 *poutbuf = buf;
00103 *poutbuf_size = buf_size;
00104 return next;
00105 }
00106
00107 AVCodecParser ff_aac_latm_parser = {
00108 .codec_ids = { CODEC_ID_AAC_LATM },
00109 .priv_data_size = sizeof(LATMParseContext),
00110 .parser_parse = latm_parse,
00111 .parser_close = ff_parse_close
00112 };