FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavformat/pmpdec.c
Date: 2024-04-25 15:36:26
Exec Total Coverage
Lines: 82 111 73.9%
Functions: 4 5 80.0%
Branches: 28 42 66.7%

Line Branch Exec Source
1 /*
2 * PMP demuxer.
3 * Copyright (c) 2011 Reimar Döffinger
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/intreadwrite.h"
23 #include "libavutil/mem.h"
24 #include "avformat.h"
25 #include "demux.h"
26 #include "internal.h"
27
28 typedef struct {
29 int cur_stream;
30 int num_streams;
31 int audio_packets;
32 int current_packet;
33 uint32_t *packet_sizes;
34 int packet_sizes_alloc;
35 } PMPContext;
36
37 7128 static int pmp_probe(const AVProbeData *p) {
38
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 7127 times.
7128 if (AV_RN32(p->buf) == AV_RN32("pmpm") &&
39
1/2
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
1 AV_RL32(p->buf + 4) == 1)
40 1 return AVPROBE_SCORE_MAX;
41 7127 return 0;
42 }
43
44 1 static int pmp_header(AVFormatContext *s)
45 {
46 1 PMPContext *pmp = s->priv_data;
47 1 AVIOContext *pb = s->pb;
48 int tb_num, tb_den;
49 uint32_t index_cnt;
50 1 int audio_codec_id = AV_CODEC_ID_NONE;
51 int srate, channels;
52 unsigned i;
53 uint64_t pos;
54 1 int64_t fsize = avio_size(pb);
55
56 1 AVStream *vst = avformat_new_stream(s, NULL);
57
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 if (!vst)
58 return AVERROR(ENOMEM);
59 1 vst->codecpar->codec_type = AVMEDIA_TYPE_VIDEO;
60 1 avio_skip(pb, 8);
61
1/3
✓ Branch 1 taken 1 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
1 switch (avio_rl32(pb)) {
62 1 case 0:
63 1 vst->codecpar->codec_id = AV_CODEC_ID_MPEG4;
64 1 break;
65 case 1:
66 vst->codecpar->codec_id = AV_CODEC_ID_H264;
67 break;
68 default:
69 av_log(s, AV_LOG_ERROR, "Unsupported video format\n");
70 break;
71 }
72 1 index_cnt = avio_rl32(pb);
73 1 vst->codecpar->width = avio_rl32(pb);
74 1 vst->codecpar->height = avio_rl32(pb);
75
76 1 tb_num = avio_rl32(pb);
77 1 tb_den = avio_rl32(pb);
78 1 avpriv_set_pts_info(vst, 32, tb_num, tb_den);
79 1 vst->nb_frames = index_cnt;
80 1 vst->duration = index_cnt;
81
82
1/3
✓ Branch 1 taken 1 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
1 switch (avio_rl32(pb)) {
83 1 case 0:
84 1 audio_codec_id = AV_CODEC_ID_MP3;
85 1 break;
86 case 1:
87 av_log(s, AV_LOG_ERROR, "AAC not yet correctly supported\n");
88 audio_codec_id = AV_CODEC_ID_AAC;
89 break;
90 default:
91 av_log(s, AV_LOG_ERROR, "Unsupported audio format\n");
92 break;
93 }
94 1 pmp->num_streams = avio_rl16(pb) + 1;
95 1 avio_skip(pb, 10);
96 1 srate = avio_rl32(pb);
97 1 channels = avio_rl32(pb) + 1;
98 1 pos = avio_tell(pb) + 4LL*index_cnt;
99
2/2
✓ Branch 0 taken 1319 times.
✓ Branch 1 taken 1 times.
1320 for (i = 0; i < index_cnt; i++) {
100 1319 uint32_t size = avio_rl32(pb);
101 1319 int flags = size & 1 ? AVINDEX_KEYFRAME : 0;
102
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1319 times.
1319 if (avio_feof(pb)) {
103 av_log(s, AV_LOG_FATAL, "Encountered EOF while reading index.\n");
104 return AVERROR_INVALIDDATA;
105 }
106 1319 size >>= 1;
107
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1319 times.
1319 if (size < 9 + 4*pmp->num_streams) {
108 av_log(s, AV_LOG_ERROR, "Packet too small\n");
109 return AVERROR_INVALIDDATA;
110 }
111 1319 av_add_index_entry(vst, pos, i, size, 0, flags);
112 1319 pos += size;
113
4/6
✓ Branch 0 taken 1319 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
✓ Branch 3 taken 1318 times.
✗ Branch 4 not taken.
✓ Branch 5 taken 1 times.
1319 if (fsize > 0 && i == 0 && pos > fsize) {
114 av_log(s, AV_LOG_ERROR, "File ends before first packet\n");
115 return AVERROR_INVALIDDATA;
116 }
117 }
118
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 1 times.
2 for (i = 1; i < pmp->num_streams; i++) {
119 1 AVStream *ast = avformat_new_stream(s, NULL);
120
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 if (!ast)
121 return AVERROR(ENOMEM);
122 1 ast->codecpar->codec_type = AVMEDIA_TYPE_AUDIO;
123 1 ast->codecpar->codec_id = audio_codec_id;
124 1 ast->codecpar->ch_layout.nb_channels = channels;
125 1 ast->codecpar->sample_rate = srate;
126 1 avpriv_set_pts_info(ast, 32, 1, srate);
127 }
128 1 return 0;
129 }
130
131 189 static int pmp_packet(AVFormatContext *s, AVPacket *pkt)
132 {
133 189 PMPContext *pmp = s->priv_data;
134 189 AVIOContext *pb = s->pb;
135 189 int ret = 0;
136 int i;
137
138
2/2
✓ Branch 1 taken 1 times.
✓ Branch 2 taken 188 times.
189 if (avio_feof(pb))
139 1 return AVERROR_EOF;
140
2/2
✓ Branch 0 taken 83 times.
✓ Branch 1 taken 105 times.
188 if (pmp->cur_stream == 0) {
141 int num_packets;
142 83 pmp->audio_packets = avio_r8(pb);
143
144
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 83 times.
83 if (!pmp->audio_packets) {
145 av_log(s, AV_LOG_ERROR, "No audio packets.\n");
146 return AVERROR_INVALIDDATA;
147 }
148
149 83 num_packets = (pmp->num_streams - 1) * pmp->audio_packets + 1;
150 83 avio_skip(pb, 8);
151 83 pmp->current_packet = 0;
152 83 av_fast_malloc(&pmp->packet_sizes,
153 83 &pmp->packet_sizes_alloc,
154 num_packets * sizeof(*pmp->packet_sizes));
155
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 83 times.
83 if (!pmp->packet_sizes_alloc) {
156 av_log(s, AV_LOG_ERROR, "Cannot (re)allocate packet buffer\n");
157 return AVERROR(ENOMEM);
158 }
159
2/2
✓ Branch 0 taken 190 times.
✓ Branch 1 taken 83 times.
273 for (i = 0; i < num_packets; i++)
160 190 pmp->packet_sizes[i] = avio_rl32(pb);
161 }
162 188 ret = av_get_packet(pb, pkt, pmp->packet_sizes[pmp->current_packet]);
163
1/2
✓ Branch 0 taken 188 times.
✗ Branch 1 not taken.
188 if (ret >= 0) {
164 188 ret = 0;
165 188 pkt->stream_index = pmp->cur_stream;
166 }
167
2/2
✓ Branch 0 taken 165 times.
✓ Branch 1 taken 23 times.
188 if (pmp->current_packet % pmp->audio_packets == 0)
168 165 pmp->cur_stream = (pmp->cur_stream + 1) % pmp->num_streams;
169 188 pmp->current_packet++;
170 188 return ret;
171 }
172
173 static int pmp_seek(AVFormatContext *s, int stream_index, int64_t ts, int flags)
174 {
175 PMPContext *pmp = s->priv_data;
176 pmp->cur_stream = 0;
177 // fall back on default seek now
178 return -1;
179 }
180
181 1 static int pmp_close(AVFormatContext *s)
182 {
183 1 PMPContext *pmp = s->priv_data;
184 1 av_freep(&pmp->packet_sizes);
185 1 return 0;
186 }
187
188 const FFInputFormat ff_pmp_demuxer = {
189 .p.name = "pmp",
190 .p.long_name = NULL_IF_CONFIG_SMALL("Playstation Portable PMP"),
191 .priv_data_size = sizeof(PMPContext),
192 .read_probe = pmp_probe,
193 .read_header = pmp_header,
194 .read_packet = pmp_packet,
195 .read_seek = pmp_seek,
196 .read_close = pmp_close,
197 };
198