00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00026 #include <stdio.h>
00027 #include <stdlib.h>
00028 #include <string.h>
00029 #include <math.h>
00030 #include <sys/types.h>
00031
00032 #include "parser.h"
00033
00034 #define LATM_HEADER 0x56e000 // 0x2b7 (11 bits)
00035 #define LATM_MASK 0xFFE000 // top 11 bits
00036 #define LATM_SIZE_MASK 0x001FFF // bottom 13 bits
00037
00038 typedef struct LATMParseContext{
00039 ParseContext pc;
00040 int count;
00041 } LATMParseContext;
00042
00047 static int latm_find_frame_end(AVCodecParserContext *s1, const uint8_t *buf,
00048 int buf_size)
00049 {
00050 LATMParseContext *s = s1->priv_data;
00051 ParseContext *pc = &s->pc;
00052 int pic_found, i;
00053 uint32_t state;
00054
00055 pic_found = pc->frame_start_found;
00056 state = pc->state;
00057
00058 i = 0;
00059 if (!pic_found) {
00060 for (i = 0; i < buf_size; i++) {
00061 state = (state<<8) | buf[i];
00062 if ((state & LATM_MASK) == LATM_HEADER) {
00063 i++;
00064 s->count = -i;
00065 pic_found = 1;
00066 break;
00067 }
00068 }
00069 }
00070
00071 if (pic_found) {
00072
00073 if (buf_size == 0)
00074 return 0;
00075 if ((state & LATM_SIZE_MASK) - s->count <= buf_size) {
00076 pc->frame_start_found = 0;
00077 pc->state = -1;
00078 return (state & LATM_SIZE_MASK) - s->count;
00079 }
00080 }
00081
00082 s->count += buf_size;
00083 pc->frame_start_found = pic_found;
00084 pc->state = state;
00085
00086 return END_NOT_FOUND;
00087 }
00088
00089 static int latm_parse(AVCodecParserContext *s1, AVCodecContext *avctx,
00090 const uint8_t **poutbuf, int *poutbuf_size,
00091 const uint8_t *buf, int buf_size)
00092 {
00093 LATMParseContext *s = s1->priv_data;
00094 ParseContext *pc = &s->pc;
00095 int next;
00096
00097 if (s1->flags & PARSER_FLAG_COMPLETE_FRAMES) {
00098 next = buf_size;
00099 } else {
00100 next = latm_find_frame_end(s1, buf, buf_size);
00101
00102 if (ff_combine_frame(pc, next, &buf, &buf_size) < 0) {
00103 *poutbuf = NULL;
00104 *poutbuf_size = 0;
00105 return buf_size;
00106 }
00107 }
00108 *poutbuf = buf;
00109 *poutbuf_size = buf_size;
00110 return next;
00111 }
00112
00113 AVCodecParser aac_latm_parser = {
00114 { CODEC_ID_AAC_LATM },
00115 sizeof(LATMParseContext),
00116 NULL,
00117 latm_parse,
00118 ff_parse_close
00119 };