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 
29 #define LATM_HEADER 0x56e000 // 0x2b7 (11 bits)
30 #define LATM_MASK 0xFFE000 // top 11 bits
31 #define LATM_SIZE_MASK 0x001FFF // bottom 13 bits
32 
33 typedef struct LATMParseContext{
35  int count;
37 
38 /**
39  * Find the end of the current frame in the bitstream.
40  * @return the position of the first byte of the next frame, or -1
41  */
42 static int latm_find_frame_end(AVCodecParserContext *s1, const uint8_t *buf,
43  int buf_size)
44 {
45  LATMParseContext *s = s1->priv_data;
46  ParseContext *pc = &s->pc;
47  int pic_found, i;
48  uint32_t state;
49 
50  pic_found = pc->frame_start_found;
51  state = pc->state;
52 
53  if (!pic_found) {
54  for (i = 0; i < buf_size; i++) {
55  state = (state<<8) | buf[i];
56  if ((state & LATM_MASK) == LATM_HEADER) {
57  i++;
58  s->count = -i;
59  pic_found = 1;
60  break;
61  }
62  }
63  }
64 
65  if (pic_found) {
66  /* EOF considered as end of frame */
67  if (buf_size == 0)
68  return 0;
69  if ((state & LATM_SIZE_MASK) - s->count <= buf_size) {
70  pc->frame_start_found = 0;
71  pc->state = -1;
72  return (state & LATM_SIZE_MASK) - s->count;
73  }
74  }
75 
76  s->count += buf_size;
77  pc->frame_start_found = pic_found;
78  pc->state = state;
79 
80  return END_NOT_FOUND;
81 }
82 
84  const uint8_t **poutbuf, int *poutbuf_size,
85  const uint8_t *buf, int buf_size)
86 {
87  LATMParseContext *s = s1->priv_data;
88  ParseContext *pc = &s->pc;
89  int next;
90 
91  if (s1->flags & PARSER_FLAG_COMPLETE_FRAMES) {
92  next = buf_size;
93  } else {
94  next = latm_find_frame_end(s1, buf, buf_size);
95 
96  if (ff_combine_frame(pc, next, &buf, &buf_size) < 0) {
97  *poutbuf = NULL;
98  *poutbuf_size = 0;
99  return buf_size;
100  }
101  }
102  *poutbuf = buf;
103  *poutbuf_size = buf_size;
104  return next;
105 }
106 
109  .priv_data_size = sizeof(LATMParseContext),
110  .parser_parse = latm_parse,
111  .parser_close = ff_parse_close
112 };
LATMParseContext::pc
ParseContext pc
Definition: latm_parser.c:34
ff_parse_close
void ff_parse_close(AVCodecParserContext *s)
Definition: parser.c:286
LATM_SIZE_MASK
#define LATM_SIZE_MASK
Definition: latm_parser.c:31
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:42
ParseContext::state
uint32_t state
contains the last few bytes in MSB order
Definition: parser.h:33
ParseContext
Definition: parser.h:28
s
#define s(width, name)
Definition: cbs_vp9.c:257
s1
#define s1
Definition: regdef.h:38
LATM_HEADER
#define LATM_HEADER
Definition: latm_parser.c:29
NULL
#define NULL
Definition: coverity.c:32
ParseContext::frame_start_found
int frame_start_found
Definition: parser.h:34
ff_aac_latm_parser
const AVCodecParser ff_aac_latm_parser
Definition: latm_parser.c:107
state
static struct @320 state
AVCodecParser::codec_ids
int codec_ids[7]
Definition: avcodec.h:2935
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:201
LATM_MASK
#define LATM_MASK
Definition: latm_parser.c:30
PARSER_FLAG_COMPLETE_FRAMES
#define PARSER_FLAG_COMPLETE_FRAMES
Definition: avcodec.h:2809
i
#define i(width, name, range_min, range_max)
Definition: cbs_h2645.c:271
LATMParseContext
Definition: latm_parser.c:33
parser.h
AVCodecParserContext
Definition: avcodec.h:2775
AVCodecContext
main external API structure.
Definition: avcodec.h:383
END_NOT_FOUND
#define END_NOT_FOUND
Definition: parser.h:40
AVCodecParser
Definition: avcodec.h:2934
AV_CODEC_ID_AAC_LATM
@ AV_CODEC_ID_AAC_LATM
Definition: codec_id.h:472
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:83
LATMParseContext::count
int count
Definition: latm_parser.c:35