FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavcodec/cavs_parser.c
Date: 2026-09-27 22:20:00
Exec Total Coverage
Lines: 81 87 93.1%
Functions: 4 4 100.0%
Branches: 34 43 79.1%

Line Branch Exec Source
1 /*
2 * Chinese AVS video (AVS1-P2, JiZhun profile) parser.
3 * Copyright (c) 2006 Stefan Gehrer <stefan.gehrer@gmx.de>
4 *
5 * This file is part of FFmpeg.
6 *
7 * FFmpeg is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
11 *
12 * FFmpeg is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
16 *
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with FFmpeg; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20 */
21
22 /**
23 * @file
24 * Chinese AVS video (AVS1-P2, JiZhun profile) parser
25 * @author Stefan Gehrer <stefan.gehrer@gmx.de>
26 */
27
28 #include "parser.h"
29 #include "cavs.h"
30 #include "get_bits.h"
31 #include "mpeg12data.h"
32 #include "parser_internal.h"
33 #include "startcode.h"
34
35
36 /**
37 * Find the end of the current frame in the bitstream.
38 * @return the position of the first byte of the next frame, or -1
39 */
40 3118 static int cavs_find_frame_end(ParseContext *pc, const uint8_t *buf,
41 int buf_size) {
42 int pic_found, i;
43 uint32_t state;
44
45 3118 pic_found= pc->frame_start_found;
46 3118 state= pc->state;
47
48 3118 i=0;
49
2/2
✓ Branch 0 taken 526 times.
✓ Branch 1 taken 2592 times.
3118 if(!pic_found){
50
1/2
✓ Branch 0 taken 2558 times.
✗ Branch 1 not taken.
2558 for(i=0; i<buf_size; i++){
51 2558 state= (state<<8) | buf[i];
52
4/4
✓ Branch 0 taken 2539 times.
✓ Branch 1 taken 19 times.
✓ Branch 2 taken 507 times.
✓ Branch 3 taken 2032 times.
2558 if(state == PIC_I_START_CODE || state == PIC_PB_START_CODE){
53 526 i++;
54 526 pic_found=1;
55 526 break;
56 }
57 }
58 }
59
60
1/2
✓ Branch 0 taken 3118 times.
✗ Branch 1 not taken.
3118 if(pic_found){
61 /* EOF considered as end of frame */
62
2/2
✓ Branch 0 taken 4 times.
✓ Branch 1 taken 3114 times.
3118 if (buf_size == 0)
63 4 return 0;
64
2/2
✓ Branch 0 taken 5034147 times.
✓ Branch 1 taken 2593 times.
5036740 for(; i<buf_size; i++){
65 5034147 state= (state<<8) | buf[i];
66
5/6
✓ Branch 0 taken 5034147 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 5033640 times.
✓ Branch 3 taken 507 times.
✓ Branch 4 taken 14 times.
✓ Branch 5 taken 5033626 times.
5034147 if (state == PIC_I_START_CODE || state == PIC_PB_START_CODE ||
67 state == CAVS_START_CODE) {
68 521 pc->frame_start_found=0;
69 521 pc->state=-1;
70 521 return i-3;
71 }
72 }
73 }
74 2593 pc->frame_start_found= pic_found;
75 2593 pc->state= state;
76 2593 return END_NOT_FOUND;
77 }
78
79 19 static int parse_seq_header(AVCodecParserContext *s, AVCodecContext *avctx,
80 GetBitContext *gb)
81 {
82 int frame_rate_code;
83 int width, height;
84 int mb_width, mb_height;
85
86 19 skip_bits(gb, 8); // profile
87 19 skip_bits(gb, 8); // level
88 19 skip_bits1(gb); // progressive sequence
89
90 19 width = get_bits(gb, 14);
91 19 height = get_bits(gb, 14);
92
2/4
✓ Branch 0 taken 19 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 19 times.
19 if (width <= 0 || height <= 0) {
93 ✗ av_log(avctx, AV_LOG_ERROR, "Dimensions invalid\n");
94 ✗ return AVERROR_INVALIDDATA;
95 }
96 19 mb_width = (width + 15) >> 4;
97 19 mb_height = (height + 15) >> 4;
98
99 19 skip_bits(gb, 2); // chroma format
100 19 skip_bits(gb, 3); // sample_precision
101 19 skip_bits(gb, 4); // aspect_ratio
102 19 frame_rate_code = get_bits(gb, 4);
103
2/4
✓ Branch 0 taken 19 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 19 times.
19 if (frame_rate_code == 0 || frame_rate_code > 13) {
104 ✗ av_log(avctx, AV_LOG_WARNING,
105 "frame_rate_code %d is invalid\n", frame_rate_code);
106 ✗ frame_rate_code = 1;
107 }
108
109 19 skip_bits(gb, 18); // bit_rate_lower
110 19 skip_bits1(gb); // marker_bit
111 19 skip_bits(gb, 12); // bit_rate_upper
112 19 skip_bits1(gb); // low_delay
113
114 19 s->width = width;
115 19 s->height = height;
116 19 s->coded_width = 16 * mb_width;
117 19 s->coded_height = 16 * mb_height;
118 19 avctx->framerate = ff_mpeg12_frame_rate_tab[frame_rate_code];
119
120 19 return 0;
121 }
122
123 525 static int cavs_parse_frame(AVCodecParserContext *s, AVCodecContext *avctx,
124 const uint8_t *buf, int buf_size)
125 {
126 GetBitContext gb;
127 const uint8_t *buf_end;
128 const uint8_t *buf_ptr;
129 525 uint32_t stc = -1;
130
131 525 s->key_frame = 0;
132 525 s->pict_type = AV_PICTURE_TYPE_NONE;
133
134
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 523 times.
525 if (buf_size == 0)
135 2 return 0;
136
137 523 buf_ptr = buf;
138 523 buf_end = buf + buf_size;
139 for (;;) {
140 1133 buf_ptr = avpriv_find_start_code(buf_ptr, buf_end, &stc);
141
4/4
✓ Branch 0 taken 611 times.
✓ Branch 1 taken 522 times.
✓ Branch 2 taken 1 times.
✓ Branch 3 taken 610 times.
1133 if ((stc & 0xFFFFFE00) || buf_ptr == buf_end)
142 523 return 0;
143
3/3
✓ Branch 0 taken 19 times.
✓ Branch 1 taken 19 times.
✓ Branch 2 taken 572 times.
610 switch (stc) {
144 19 case CAVS_START_CODE:
145
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 19 times.
19 if (init_get_bits8(&gb, buf_ptr, buf_end - buf_ptr) < 0)
146 ✗ return 0;
147 19 parse_seq_header(s, avctx, &gb);
148 19 break;
149 19 case PIC_I_START_CODE:
150 19 s->key_frame = 1;
151 19 s->pict_type = AV_PICTURE_TYPE_I;
152 19 break;
153 572 default:
154 572 break;
155 }
156 }
157 }
158
159 3118 static int cavsvideo_parse(AVCodecParserContext *s,
160 AVCodecContext *avctx,
161 const uint8_t **poutbuf, int *poutbuf_size,
162 const uint8_t *buf, int buf_size)
163 {
164 3118 ParseContext *pc = s->priv_data;
165 int next;
166
167
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 3118 times.
3118 if(s->flags & PARSER_FLAG_COMPLETE_FRAMES){
168 ✗ next= buf_size;
169 }else{
170 3118 next= cavs_find_frame_end(pc, buf, buf_size);
171
172
2/2
✓ Branch 1 taken 2593 times.
✓ Branch 2 taken 525 times.
3118 if (ff_combine_frame(pc, next, &buf, &buf_size) < 0) {
173 2593 *poutbuf = NULL;
174 2593 *poutbuf_size = 0;
175 2593 return buf_size;
176 }
177 }
178
179 525 cavs_parse_frame(s, avctx, buf, buf_size);
180
181 525 *poutbuf = buf;
182 525 *poutbuf_size = buf_size;
183 525 return next;
184 }
185
186 const FFCodecParser ff_cavsvideo_parser = {
187 PARSER_CODEC_LIST(AV_CODEC_ID_CAVS),
188 .priv_data_size = sizeof(ParseContext),
189 .parse = cavsvideo_parse,
190 .close = ff_parse_close,
191 };
192