FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavformat/mxg.c
Date: 2024-09-07 18:49:03
Exec Total Coverage
Lines: 113 130 86.9%
Functions: 5 5 100.0%
Branches: 62 86 72.1%

Line Branch Exec Source
1 /*
2 * MxPEG clip file demuxer
3 * Copyright (c) 2010 Anatoly Nenashev
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 #include "libavutil/channel_layout.h"
23 #include "libavutil/internal.h"
24 #include "libavutil/intreadwrite.h"
25 #include "libavutil/mem.h"
26 #include "libavcodec/mjpeg.h"
27 #include "avformat.h"
28 #include "demux.h"
29 #include "internal.h"
30 #include "avio.h"
31
32 #define DEFAULT_PACKET_SIZE 1024
33 #define OVERREAD_SIZE 3
34
35 typedef struct MXGContext {
36 uint8_t *buffer;
37 uint8_t *buffer_ptr;
38 uint8_t *soi_ptr;
39 unsigned int buffer_size;
40 int64_t dts;
41 unsigned int cache_size;
42 } MXGContext;
43
44 1 static int mxg_read_header(AVFormatContext *s)
45 {
46 AVStream *video_st, *audio_st;
47 1 MXGContext *mxg = s->priv_data;
48
49 /* video parameters will be extracted from the compressed bitstream */
50 1 video_st = avformat_new_stream(s, NULL);
51
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 if (!video_st)
52 return AVERROR(ENOMEM);
53 1 video_st->codecpar->codec_type = AVMEDIA_TYPE_VIDEO;
54 1 video_st->codecpar->codec_id = AV_CODEC_ID_MXPEG;
55 1 avpriv_set_pts_info(video_st, 64, 1, 1000000);
56
57 1 audio_st = avformat_new_stream(s, NULL);
58
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 if (!audio_st)
59 return AVERROR(ENOMEM);
60 1 audio_st->codecpar->codec_type = AVMEDIA_TYPE_AUDIO;
61 1 audio_st->codecpar->codec_id = AV_CODEC_ID_PCM_ALAW;
62 1 audio_st->codecpar->ch_layout = (AVChannelLayout)AV_CHANNEL_LAYOUT_MONO;
63 1 audio_st->codecpar->sample_rate = 8000;
64 1 audio_st->codecpar->bits_per_coded_sample = 8;
65 1 audio_st->codecpar->block_align = 1;
66 1 avpriv_set_pts_info(audio_st, 64, 1, 1000000);
67
68 1 mxg->soi_ptr = mxg->buffer_ptr = mxg->buffer = 0;
69 1 mxg->buffer_size = 0;
70 1 mxg->dts = AV_NOPTS_VALUE;
71 1 mxg->cache_size = 0;
72
73 1 return 0;
74 }
75
76 8365 static uint8_t* mxg_find_startmarker(uint8_t *p, uint8_t *end)
77 {
78
2/2
✓ Branch 0 taken 138625 times.
✓ Branch 1 taken 524 times.
139149 for (; p < end - 3; p += 4) {
79 138625 uint32_t x = AV_RN32(p);
80
81
2/2
✓ Branch 0 taken 7841 times.
✓ Branch 1 taken 130784 times.
138625 if (x & (~(x+0x01010101)) & 0x80808080) {
82
2/2
✓ Branch 0 taken 1627 times.
✓ Branch 1 taken 6214 times.
7841 if (p[0] == 0xff) {
83 1627 return p;
84
2/2
✓ Branch 0 taken 1718 times.
✓ Branch 1 taken 4496 times.
6214 } else if (p[1] == 0xff) {
85 1718 return p+1;
86
2/2
✓ Branch 0 taken 2815 times.
✓ Branch 1 taken 1681 times.
4496 } else if (p[2] == 0xff) {
87 2815 return p+2;
88
1/2
✓ Branch 0 taken 1681 times.
✗ Branch 1 not taken.
1681 } else if (p[3] == 0xff) {
89 1681 return p+3;
90 }
91 }
92 }
93
94
2/2
✓ Branch 0 taken 763 times.
✓ Branch 1 taken 511 times.
1274 for (; p < end; ++p) {
95
2/2
✓ Branch 0 taken 13 times.
✓ Branch 1 taken 750 times.
763 if (*p == 0xff) return p;
96 }
97
98 511 return end;
99 }
100
101 590 static int mxg_update_cache(AVFormatContext *s, unsigned int cache_size)
102 {
103 590 MXGContext *mxg = s->priv_data;
104 590 unsigned int current_pos = mxg->buffer_ptr - mxg->buffer;
105 unsigned int soi_pos;
106 uint8_t *buffer;
107 int ret;
108
109 /* reallocate internal buffer */
110
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 590 times.
590 if (current_pos > current_pos + cache_size)
111 return AVERROR(ENOMEM);
112 590 soi_pos = mxg->soi_ptr - mxg->buffer;
113 590 buffer = av_fast_realloc(mxg->buffer, &mxg->buffer_size,
114 590 current_pos + cache_size +
115 AV_INPUT_BUFFER_PADDING_SIZE);
116
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 590 times.
590 if (!buffer)
117 return AVERROR(ENOMEM);
118 590 mxg->buffer = buffer;
119 590 mxg->buffer_ptr = mxg->buffer + current_pos;
120
2/2
✓ Branch 0 taken 587 times.
✓ Branch 1 taken 3 times.
590 if (mxg->soi_ptr) mxg->soi_ptr = mxg->buffer + soi_pos;
121
122 /* get data */
123 590 ret = avio_read(s->pb, mxg->buffer_ptr + mxg->cache_size,
124 590 cache_size - mxg->cache_size);
125
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 590 times.
590 if (ret < 0)
126 return ret;
127
128 590 mxg->cache_size += ret;
129
130 590 return ret;
131 }
132
133 32 static int mxg_read_packet(AVFormatContext *s, AVPacket *pkt)
134 {
135 int ret;
136 unsigned int size;
137 uint8_t *startmarker_ptr, *end, *search_end, marker;
138 32 MXGContext *mxg = s->priv_data;
139
140
3/4
✓ Branch 1 taken 8365 times.
✓ Branch 2 taken 1 times.
✓ Branch 3 taken 8365 times.
✗ Branch 4 not taken.
8366 while (!avio_feof(s->pb) && !s->pb->error){
141
2/2
✓ Branch 0 taken 560 times.
✓ Branch 1 taken 7805 times.
8365 if (mxg->cache_size <= OVERREAD_SIZE) {
142 /* update internal buffer */
143 560 ret = mxg_update_cache(s, DEFAULT_PACKET_SIZE + OVERREAD_SIZE);
144
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 560 times.
560 if (ret < 0)
145 return ret;
146 }
147 8365 end = mxg->buffer_ptr + mxg->cache_size;
148
149 /* find start marker - 0xff */
150
1/2
✓ Branch 0 taken 8365 times.
✗ Branch 1 not taken.
8365 if (mxg->cache_size > OVERREAD_SIZE) {
151 8365 search_end = end - OVERREAD_SIZE;
152 8365 startmarker_ptr = mxg_find_startmarker(mxg->buffer_ptr, search_end);
153 } else {
154 search_end = end;
155 startmarker_ptr = mxg_find_startmarker(mxg->buffer_ptr, search_end);
156 if (startmarker_ptr >= search_end - 1 ||
157 *(startmarker_ptr + 1) != EOI) break;
158 }
159
160
2/2
✓ Branch 0 taken 7854 times.
✓ Branch 1 taken 511 times.
8365 if (startmarker_ptr != search_end) { /* start marker found */
161 7854 marker = *(startmarker_ptr + 1);
162 7854 mxg->buffer_ptr = startmarker_ptr + 2;
163 7854 mxg->cache_size = end - mxg->buffer_ptr;
164
165
2/2
✓ Branch 0 taken 31 times.
✓ Branch 1 taken 7823 times.
7854 if (marker == SOI) {
166 31 mxg->soi_ptr = startmarker_ptr;
167
2/2
✓ Branch 0 taken 30 times.
✓ Branch 1 taken 7793 times.
7823 } else if (marker == EOI) {
168
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 30 times.
30 if (!mxg->soi_ptr) {
169 av_log(s, AV_LOG_WARNING, "Found EOI before SOI, skipping\n");
170 continue;
171 }
172
173 30 size = mxg->buffer_ptr - mxg->soi_ptr;
174 30 ret = av_new_packet(pkt, size);
175
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 30 times.
30 if (ret < 0)
176 return ret;
177 30 memcpy(pkt->data, mxg->soi_ptr, size);
178
179 30 pkt->pts = pkt->dts = mxg->dts;
180 30 pkt->stream_index = 0;
181
182
2/2
✓ Branch 0 taken 14 times.
✓ Branch 1 taken 16 times.
30 if (mxg->soi_ptr - mxg->buffer > mxg->cache_size) {
183
1/2
✓ Branch 0 taken 14 times.
✗ Branch 1 not taken.
14 if (mxg->cache_size > 0) {
184 14 memmove(mxg->buffer, mxg->buffer_ptr, mxg->cache_size);
185 }
186
187 14 mxg->buffer_ptr = mxg->buffer;
188 }
189 30 mxg->soi_ptr = 0;
190
191 30 return pkt->size;
192
6/6
✓ Branch 0 taken 133 times.
✓ Branch 1 taken 7660 times.
✓ Branch 2 taken 128 times.
✓ Branch 3 taken 5 times.
✓ Branch 4 taken 128 times.
✓ Branch 5 taken 7660 times.
7793 } else if ( (SOF0 <= marker && marker <= SOF15) ||
193
1/2
✓ Branch 0 taken 128 times.
✗ Branch 1 not taken.
128 (SOS <= marker && marker <= COM) ) {
194 /* all other markers that start marker segment also contain
195 length value (see specification for JPEG Annex B.1) */
196 133 size = AV_RB16(mxg->buffer_ptr);
197
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 133 times.
133 if (size < 2)
198 return AVERROR(EINVAL);
199
200
2/2
✓ Branch 0 taken 30 times.
✓ Branch 1 taken 103 times.
133 if (mxg->cache_size < size) {
201 30 ret = mxg_update_cache(s, size);
202
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 30 times.
30 if (ret < 0)
203 return ret;
204 30 startmarker_ptr = mxg->buffer_ptr - 2;
205 30 mxg->cache_size = 0;
206 } else {
207 103 mxg->cache_size -= size;
208 }
209
210 133 mxg->buffer_ptr += size;
211
212
3/4
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 132 times.
✓ Branch 2 taken 1 times.
✗ Branch 3 not taken.
133 if (marker == APP13 && size >= 16) { /* audio data */
213 1 ret = av_new_packet(pkt, size - 14);
214
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 if (ret < 0)
215 return ret;
216 1 memcpy(pkt->data, startmarker_ptr + 16, size - 14);
217
218 /* time (GMT) of first sample in usec since 1970, little-endian */
219 1 pkt->pts = pkt->dts = AV_RL64(startmarker_ptr + 8);
220 1 pkt->stream_index = 1;
221
222
1/2
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
1 if (startmarker_ptr - mxg->buffer > mxg->cache_size) {
223
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 if (mxg->cache_size > 0) {
224 memcpy(mxg->buffer, mxg->buffer_ptr, mxg->cache_size);
225 }
226 1 mxg->buffer_ptr = mxg->buffer;
227 }
228
229 1 return pkt->size;
230
3/4
✓ Branch 0 taken 63 times.
✓ Branch 1 taken 69 times.
✓ Branch 2 taken 63 times.
✗ Branch 3 not taken.
132 } else if (marker == COM && size >= 18 &&
231
2/2
✓ Branch 0 taken 31 times.
✓ Branch 1 taken 32 times.
63 !strncmp(startmarker_ptr + 4, "MXF", 3)) {
232 /* time (GMT) of video frame in usec since 1970, little-endian */
233 31 mxg->dts = AV_RL64(startmarker_ptr + 12);
234 }
235 }
236 } else {
237 /* start marker not found */
238 511 mxg->buffer_ptr = search_end;
239 511 mxg->cache_size = OVERREAD_SIZE;
240 }
241 }
242
243 1 return AVERROR_EOF;
244 }
245
246 1 static int mxg_close(struct AVFormatContext *s)
247 {
248 1 MXGContext *mxg = s->priv_data;
249 1 av_freep(&mxg->buffer);
250 1 return 0;
251 }
252
253 const FFInputFormat ff_mxg_demuxer = {
254 .p.name = "mxg",
255 .p.long_name = NULL_IF_CONFIG_SMALL("MxPEG clip"),
256 .p.extensions = "mxg",
257 .priv_data_size = sizeof(MXGContext),
258 .read_header = mxg_read_header,
259 .read_packet = mxg_read_packet,
260 .read_close = mxg_close,
261 };
262