FFmpeg
latm_parser.c
Go to the documentation of this file.
1 /*
2  * copyright (c) 2008 Paul Kendall <paul@kcbbs.gen.nz>
3  *
4  * This file is part of FFmpeg.
5  *
6  * FFmpeg is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or (at your option) any later version.
10  *
11  * FFmpeg is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with FFmpeg; if not, write to the Free Software
18  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19  */
20 
21 /**
22  * @file
23  * AAC LATM parser
24  */
25 
26 #include <stdint.h>
27 #include "parser.h"
28 #include "parser_internal.h"
29 
30 #define LATM_HEADER 0x56e000 // 0x2b7 (11 bits)
31 #define LATM_MASK 0xFFE000 // top 11 bits
32 #define LATM_SIZE_MASK 0x001FFF // bottom 13 bits
33 
34 typedef struct LATMParseContext{
36  int count;
38 
39 /**
40  * Find the end of the current frame in the bitstream.
41  * @return the position of the first byte of the next frame, or -1
42  */
43 static int latm_find_frame_end(AVCodecParserContext *s1, const uint8_t *buf,
44  int buf_size)
45 {
47  ParseContext *pc = &s->pc;
48  int pic_found, i;
49  uint32_t state;
50 
51  pic_found = pc->frame_start_found;
52  state = pc->state;
53 
54  if (!pic_found) {
55  for (i = 0; i < buf_size; i++) {
56  state = (state<<8) | buf[i];
57  if ((state & LATM_MASK) == LATM_HEADER) {
58  i++;
59  s->count = -i;
60  pic_found = 1;
61  break;
62  }
63  }
64  }
65 
66  if (pic_found) {
67  /* EOF considered as end of frame */
68  if (buf_size == 0)
69  return 0;
70  if ((state & LATM_SIZE_MASK) - s->count <= buf_size) {
71  pc->frame_start_found = 0;
72  pc->state = -1;
73  return (state & LATM_SIZE_MASK) - s->count;
74  }
75  }
76 
77  s->count += buf_size;
78  pc->frame_start_found = pic_found;
79  pc->state = state;
80 
81  return END_NOT_FOUND;
82 }
83 
85  const uint8_t **poutbuf, int *poutbuf_size,
86  const uint8_t *buf, int buf_size)
87 {
89  ParseContext *pc = &s->pc;
90  int next;
91 
93  next = buf_size;
94  } else {
95  next = latm_find_frame_end(s1, buf, buf_size);
96 
97  if (ff_combine_frame(pc, next, &buf, &buf_size) < 0) {
98  *poutbuf = NULL;
99  *poutbuf_size = 0;
100  return buf_size;
101  }
102  }
103  *poutbuf = buf;
104  *poutbuf_size = buf_size;
105  return next;
106 }
107 
110  .priv_data_size = sizeof(LATMParseContext),
111  .parse = latm_parse,
113 };
LATMParseContext::pc
ParseContext pc
Definition: latm_parser.c:35
ff_parse_close
av_cold void ff_parse_close(AVCodecParserContext *s)
Definition: parser.c:298
parser_internal.h
LATM_SIZE_MASK
#define LATM_SIZE_MASK
Definition: latm_parser.c:32
latm_find_frame_end
static int latm_find_frame_end(AVCodecParserContext *s1, const uint8_t *buf, int buf_size)
Find the end of the current frame in the bitstream.
Definition: latm_parser.c:43
ParseContext::state
uint32_t state
contains the last few bytes in MSB order
Definition: parser.h:33
ff_aac_latm_parser
const FFCodecParser ff_aac_latm_parser
Definition: latm_parser.c:108
close
static av_cold void close(AVCodecParserContext *s)
Definition: apv_parser.c:136
ParseContext
Definition: parser.h:28
s
#define s(width, name)
Definition: cbs_vp9.c:198
LATM_HEADER
#define LATM_HEADER
Definition: latm_parser.c:30
NULL
#define NULL
Definition: coverity.c:32
state
static struct @541 state
ParseContext::frame_start_found
int frame_start_found
Definition: parser.h:34
AVCodecParserContext::flags
int flags
Definition: avcodec.h:2608
parse
static int parse(AVCodecParserContext *s, AVCodecContext *avctx, const uint8_t **poutbuf, int *poutbuf_size, const uint8_t *buf, int buf_size)
Definition: apv_parser.c:46
ff_combine_frame
int ff_combine_frame(ParseContext *pc, int next, const uint8_t **buf, int *buf_size)
Combine the (truncated) bitstream to a complete frame.
Definition: parser.c:211
LATM_MASK
#define LATM_MASK
Definition: latm_parser.c:31
FFCodecParser
Definition: parser_internal.h:29
PARSER_FLAG_COMPLETE_FRAMES
#define PARSER_FLAG_COMPLETE_FRAMES
Definition: avcodec.h:2609
i
#define i(width, name, range_min, range_max)
Definition: cbs_h2645.c:256
LATMParseContext
Definition: latm_parser.c:34
parser.h
PARSER_CODEC_LIST
#define PARSER_CODEC_LIST(...)
Definition: parser_internal.h:76
AVCodecParserContext
Definition: avcodec.h:2575
AVCodecContext
main external API structure.
Definition: avcodec.h:431
END_NOT_FOUND
#define END_NOT_FOUND
Definition: parser.h:40
AV_CODEC_ID_AAC_LATM
@ AV_CODEC_ID_AAC_LATM
Definition: codec_id.h:508
AVCodecParserContext::priv_data
void * priv_data
Definition: avcodec.h:2576
latm_parse
static int latm_parse(AVCodecParserContext *s1, AVCodecContext *avctx, const uint8_t **poutbuf, int *poutbuf_size, const uint8_t *buf, int buf_size)
Definition: latm_parser.c:84
LATMParseContext::count
int count
Definition: latm_parser.c:36