Line | Branch | Exec | Source |
---|---|---|---|
1 | /* | ||
2 | * SMJPEG demuxer | ||
3 | * Copyright (c) 2011 Paul B Mahol | ||
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 | * This is a demuxer for Loki SDL Motion JPEG files | ||
25 | */ | ||
26 | |||
27 | #include <inttypes.h> | ||
28 | |||
29 | #include "libavutil/mem.h" | ||
30 | #include "avformat.h" | ||
31 | #include "demux.h" | ||
32 | #include "internal.h" | ||
33 | #include "smjpeg.h" | ||
34 | |||
35 | typedef struct SMJPEGContext { | ||
36 | int audio_stream_index; | ||
37 | int video_stream_index; | ||
38 | } SMJPEGContext; | ||
39 | |||
40 | 7186 | static int smjpeg_probe(const AVProbeData *p) | |
41 | { | ||
42 |
2/2✓ Branch 0 taken 5 times.
✓ Branch 1 taken 7181 times.
|
7186 | if (!memcmp(p->buf, SMJPEG_MAGIC, 8)) |
43 | 5 | return AVPROBE_SCORE_MAX; | |
44 | 7181 | return 0; | |
45 | } | ||
46 | |||
47 | 5 | static int smjpeg_read_header(AVFormatContext *s) | |
48 | { | ||
49 | 5 | SMJPEGContext *sc = s->priv_data; | |
50 | 5 | AVStream *ast = NULL, *vst = NULL; | |
51 | 5 | AVIOContext *pb = s->pb; | |
52 | uint32_t version, htype, hlength, duration; | ||
53 | char *comment; | ||
54 | |||
55 | 5 | sc->audio_stream_index = | |
56 | 5 | sc->video_stream_index = -1; | |
57 | |||
58 | 5 | avio_skip(pb, 8); // magic | |
59 | 5 | version = avio_rb32(pb); | |
60 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 5 times.
|
5 | if (version) |
61 | ✗ | avpriv_request_sample(s, "Unknown version %"PRIu32, version); | |
62 | |||
63 | 5 | duration = avio_rb32(pb); // in msec | |
64 | |||
65 |
1/2✓ Branch 1 taken 16 times.
✗ Branch 2 not taken.
|
16 | while (!avio_feof(pb)) { |
66 | 16 | htype = avio_rl32(pb); | |
67 |
4/5✓ Branch 0 taken 1 times.
✓ Branch 1 taken 5 times.
✓ Branch 2 taken 5 times.
✓ Branch 3 taken 5 times.
✗ Branch 4 not taken.
|
16 | switch (htype) { |
68 | 1 | case SMJPEG_TXT: | |
69 | 1 | hlength = avio_rb32(pb); | |
70 |
2/4✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 1 times.
|
1 | if (!hlength || hlength > 512) |
71 | ✗ | return AVERROR_INVALIDDATA; | |
72 | 1 | comment = av_malloc(hlength + 1); | |
73 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
|
1 | if (!comment) |
74 | ✗ | return AVERROR(ENOMEM); | |
75 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
|
1 | if (avio_read(pb, comment, hlength) != hlength) { |
76 | ✗ | av_freep(&comment); | |
77 | ✗ | av_log(s, AV_LOG_ERROR, "error when reading comment\n"); | |
78 | ✗ | return AVERROR_INVALIDDATA; | |
79 | } | ||
80 | 1 | comment[hlength] = 0; | |
81 | 1 | av_dict_set(&s->metadata, "comment", comment, | |
82 | AV_DICT_DONT_STRDUP_VAL); | ||
83 | 1 | break; | |
84 | 5 | case SMJPEG_SND: | |
85 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 5 times.
|
5 | if (ast) { |
86 | ✗ | avpriv_request_sample(s, "Multiple audio streams"); | |
87 | ✗ | return AVERROR_PATCHWELCOME; | |
88 | } | ||
89 | 5 | hlength = avio_rb32(pb); | |
90 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 5 times.
|
5 | if (hlength < 8) |
91 | ✗ | return AVERROR_INVALIDDATA; | |
92 | 5 | ast = avformat_new_stream(s, 0); | |
93 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 5 times.
|
5 | if (!ast) |
94 | ✗ | return AVERROR(ENOMEM); | |
95 | 5 | ast->codecpar->codec_type = AVMEDIA_TYPE_AUDIO; | |
96 | 5 | ast->codecpar->sample_rate = avio_rb16(pb); | |
97 | 5 | ast->codecpar->bits_per_coded_sample = avio_r8(pb); | |
98 | 5 | ast->codecpar->ch_layout.nb_channels = avio_r8(pb); | |
99 | 5 | ast->codecpar->codec_tag = avio_rl32(pb); | |
100 | 10 | ast->codecpar->codec_id = ff_codec_get_id(ff_codec_smjpeg_audio_tags, | |
101 | 5 | ast->codecpar->codec_tag); | |
102 | 5 | ast->duration = duration; | |
103 | 5 | sc->audio_stream_index = ast->index; | |
104 | 5 | avpriv_set_pts_info(ast, 32, 1, 1000); | |
105 | 5 | avio_skip(pb, hlength - 8); | |
106 | 5 | break; | |
107 | 5 | case SMJPEG_VID: | |
108 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 5 times.
|
5 | if (vst) { |
109 | ✗ | avpriv_request_sample(s, "Multiple video streams"); | |
110 | ✗ | return AVERROR_INVALIDDATA; | |
111 | } | ||
112 | 5 | hlength = avio_rb32(pb); | |
113 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 5 times.
|
5 | if (hlength < 12) |
114 | ✗ | return AVERROR_INVALIDDATA; | |
115 | 5 | vst = avformat_new_stream(s, 0); | |
116 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 5 times.
|
5 | if (!vst) |
117 | ✗ | return AVERROR(ENOMEM); | |
118 | 5 | vst->nb_frames = avio_rb32(pb); | |
119 | 5 | vst->codecpar->codec_type = AVMEDIA_TYPE_VIDEO; | |
120 | 5 | vst->codecpar->width = avio_rb16(pb); | |
121 | 5 | vst->codecpar->height = avio_rb16(pb); | |
122 | 5 | vst->codecpar->codec_tag = avio_rl32(pb); | |
123 | 10 | vst->codecpar->codec_id = ff_codec_get_id(ff_codec_smjpeg_video_tags, | |
124 | 5 | vst->codecpar->codec_tag); | |
125 | 5 | vst->duration = duration; | |
126 | 5 | sc->video_stream_index = vst->index; | |
127 | 5 | avpriv_set_pts_info(vst, 32, 1, 1000); | |
128 | 5 | avio_skip(pb, hlength - 12); | |
129 | 5 | break; | |
130 | 5 | case SMJPEG_HEND: | |
131 | 5 | return 0; | |
132 | ✗ | default: | |
133 | ✗ | av_log(s, AV_LOG_ERROR, "unknown header %"PRIx32"\n", htype); | |
134 | ✗ | return AVERROR_INVALIDDATA; | |
135 | } | ||
136 | } | ||
137 | |||
138 | ✗ | return AVERROR_EOF; | |
139 | } | ||
140 | |||
141 | 1552 | static int smjpeg_read_packet(AVFormatContext *s, AVPacket *pkt) | |
142 | { | ||
143 | 1552 | SMJPEGContext *sc = s->priv_data; | |
144 | uint32_t dtype, size, timestamp; | ||
145 | int64_t pos; | ||
146 | int ret; | ||
147 | |||
148 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 1552 times.
|
1552 | if (avio_feof(s->pb)) |
149 | ✗ | return AVERROR_EOF; | |
150 | 1552 | pos = avio_tell(s->pb); | |
151 | 1552 | dtype = avio_rl32(s->pb); | |
152 |
4/4✓ Branch 0 taken 1262 times.
✓ Branch 1 taken 285 times.
✓ Branch 2 taken 4 times.
✓ Branch 3 taken 1 times.
|
1552 | switch (dtype) { |
153 | 1262 | case SMJPEG_SNDD: | |
154 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 1262 times.
|
1262 | if (sc->audio_stream_index < 0) |
155 | ✗ | return AVERROR_INVALIDDATA; | |
156 | 1262 | timestamp = avio_rb32(s->pb); | |
157 | 1262 | size = avio_rb32(s->pb); | |
158 | 1262 | ret = av_get_packet(s->pb, pkt, size); | |
159 | 1262 | pkt->stream_index = sc->audio_stream_index; | |
160 | 1262 | pkt->pts = timestamp; | |
161 | 1262 | pkt->pos = pos; | |
162 | 1262 | break; | |
163 | 285 | case SMJPEG_VIDD: | |
164 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 285 times.
|
285 | if (sc->video_stream_index < 0) |
165 | ✗ | return AVERROR_INVALIDDATA; | |
166 | 285 | timestamp = avio_rb32(s->pb); | |
167 | 285 | size = avio_rb32(s->pb); | |
168 | 285 | ret = av_get_packet(s->pb, pkt, size); | |
169 | 285 | pkt->stream_index = sc->video_stream_index; | |
170 | 285 | pkt->pts = timestamp; | |
171 | 285 | pkt->pos = pos; | |
172 | 285 | break; | |
173 | 4 | case SMJPEG_DONE: | |
174 | 4 | ret = AVERROR_EOF; | |
175 | 4 | break; | |
176 | 1 | default: | |
177 | 1 | av_log(s, AV_LOG_ERROR, "unknown chunk %"PRIx32"\n", dtype); | |
178 | 1 | ret = AVERROR_INVALIDDATA; | |
179 | 1 | break; | |
180 | } | ||
181 | 1552 | return ret; | |
182 | } | ||
183 | |||
184 | const FFInputFormat ff_smjpeg_demuxer = { | ||
185 | .p.name = "smjpeg", | ||
186 | .p.long_name = NULL_IF_CONFIG_SMALL("Loki SDL MJPEG"), | ||
187 | .p.extensions = "mjpg", | ||
188 | .p.flags = AVFMT_GENERIC_INDEX, | ||
189 | .priv_data_size = sizeof(SMJPEGContext), | ||
190 | .read_probe = smjpeg_probe, | ||
191 | .read_header = smjpeg_read_header, | ||
192 | .read_packet = smjpeg_read_packet, | ||
193 | }; | ||
194 |