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 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
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_ids = { CODEC_ID_AAC_LATM },
00110 .priv_data_size = sizeof(LATMParseContext),
00111 .parser_parse = latm_parse,
00112 .parser_close = ff_parse_close
00113 };