FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavcodec/latm_parser.c
Date: 2025-11-15 09:35:08
Exec Total Coverage
Lines: 36 37 97.3%
Functions: 2 2 100.0%
Branches: 15 16 93.8%

Line Branch Exec Source
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{
35 ParseContext pc;
36 int count;
37 } LATMParseContext;
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 1365 static int latm_find_frame_end(AVCodecParserContext *s1, const uint8_t *buf,
44 int buf_size)
45 {
46 1365 LATMParseContext *s = s1->priv_data;
47 1365 ParseContext *pc = &s->pc;
48 int pic_found, i;
49 uint32_t state;
50
51 1365 pic_found = pc->frame_start_found;
52 1365 state = pc->state;
53
54
2/2
✓ Branch 0 taken 1289 times.
✓ Branch 1 taken 76 times.
1365 if (!pic_found) {
55
2/2
✓ Branch 0 taken 3864 times.
✓ Branch 1 taken 1 times.
3865 for (i = 0; i < buf_size; i++) {
56 3864 state = (state<<8) | buf[i];
57
2/2
✓ Branch 0 taken 1288 times.
✓ Branch 1 taken 2576 times.
3864 if ((state & LATM_MASK) == LATM_HEADER) {
58 1288 i++;
59 1288 s->count = -i;
60 1288 pic_found = 1;
61 1288 break;
62 }
63 }
64 }
65
66
2/2
✓ Branch 0 taken 1364 times.
✓ Branch 1 taken 1 times.
1365 if (pic_found) {
67 /* EOF considered as end of frame */
68
2/2
✓ Branch 0 taken 10 times.
✓ Branch 1 taken 1354 times.
1364 if (buf_size == 0)
69 10 return 0;
70
2/2
✓ Branch 0 taken 1283 times.
✓ Branch 1 taken 71 times.
1354 if ((state & LATM_SIZE_MASK) - s->count <= buf_size) {
71 1283 pc->frame_start_found = 0;
72 1283 pc->state = -1;
73 1283 return (state & LATM_SIZE_MASK) - s->count;
74 }
75 }
76
77 72 s->count += buf_size;
78 72 pc->frame_start_found = pic_found;
79 72 pc->state = state;
80
81 72 return END_NOT_FOUND;
82 }
83
84 1365 static int latm_parse(AVCodecParserContext *s1, AVCodecContext *avctx,
85 const uint8_t **poutbuf, int *poutbuf_size,
86 const uint8_t *buf, int buf_size)
87 {
88 1365 LATMParseContext *s = s1->priv_data;
89 1365 ParseContext *pc = &s->pc;
90 int next;
91
92
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1365 times.
1365 if (s1->flags & PARSER_FLAG_COMPLETE_FRAMES) {
93 next = buf_size;
94 } else {
95 1365 next = latm_find_frame_end(s1, buf, buf_size);
96
97
2/2
✓ Branch 1 taken 71 times.
✓ Branch 2 taken 1294 times.
1365 if (ff_combine_frame(pc, next, &buf, &buf_size) < 0) {
98 71 *poutbuf = NULL;
99 71 *poutbuf_size = 0;
100 71 return buf_size;
101 }
102 }
103 1294 *poutbuf = buf;
104 1294 *poutbuf_size = buf_size;
105 1294 return next;
106 }
107
108 const FFCodecParser ff_aac_latm_parser = {
109 PARSER_CODEC_LIST(AV_CODEC_ID_AAC_LATM),
110 .priv_data_size = sizeof(LATMParseContext),
111 .parse = latm_parse,
112 .close = ff_parse_close
113 };
114