| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | /* | ||
| 2 | * VPK demuxer | ||
| 3 | * Copyright (c) 2015 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 | #include "libavutil/intreadwrite.h" | ||
| 23 | #include "avformat.h" | ||
| 24 | #include "avio_internal.h" | ||
| 25 | #include "demux.h" | ||
| 26 | #include "internal.h" | ||
| 27 | |||
| 28 | typedef struct VPKDemuxContext { | ||
| 29 | unsigned data_start; | ||
| 30 | unsigned block_count; | ||
| 31 | unsigned current_block; | ||
| 32 | unsigned last_block_size; | ||
| 33 | } VPKDemuxContext; | ||
| 34 | |||
| 35 | 8043 | static int vpk_probe(const AVProbeData *p) | |
| 36 | { | ||
| 37 |
2/2✓ Branch 0 taken 8042 times.
✓ Branch 1 taken 1 times.
|
8043 | if (AV_RL32(p->buf) != MKBETAG('V','P','K',' ')) |
| 38 | 8042 | return 0; | |
| 39 | |||
| 40 | 1 | return AVPROBE_SCORE_MAX / 3 * 2; | |
| 41 | } | ||
| 42 | |||
| 43 | 1 | static int vpk_read_header(AVFormatContext *s) | |
| 44 | { | ||
| 45 | 1 | VPKDemuxContext *vpk = s->priv_data; | |
| 46 | unsigned offset; | ||
| 47 | unsigned samples_per_block; | ||
| 48 | AVStream *st; | ||
| 49 | |||
| 50 | 1 | vpk->current_block = 0; | |
| 51 | 1 | st = avformat_new_stream(s, NULL); | |
| 52 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
|
1 | if (!st) |
| 53 | ✗ | return AVERROR(ENOMEM); | |
| 54 | |||
| 55 | 1 | avio_skip(s->pb, 4); | |
| 56 | 1 | st->duration = avio_rl32(s->pb) * 28 / 16; | |
| 57 | 1 | offset = avio_rl32(s->pb); | |
| 58 | 1 | st->codecpar->codec_type = AVMEDIA_TYPE_AUDIO; | |
| 59 | 1 | st->codecpar->codec_id = AV_CODEC_ID_ADPCM_PSX; | |
| 60 | 1 | st->codecpar->block_align = avio_rl32(s->pb); | |
| 61 | 1 | st->codecpar->sample_rate = avio_rl32(s->pb); | |
| 62 |
2/4✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 1 times.
|
1 | if (st->codecpar->sample_rate <= 0 || st->codecpar->block_align <= 0) |
| 63 | ✗ | return AVERROR_INVALIDDATA; | |
| 64 | 1 | st->codecpar->ch_layout.nb_channels = avio_rl32(s->pb); | |
| 65 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
|
1 | if (st->codecpar->ch_layout.nb_channels <= 0) |
| 66 | ✗ | return AVERROR_INVALIDDATA; | |
| 67 | 1 | samples_per_block = ((st->codecpar->block_align / st->codecpar->ch_layout.nb_channels) * 28LL) / 16; | |
| 68 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
|
1 | if (samples_per_block <= 0) |
| 69 | ✗ | return AVERROR_INVALIDDATA; | |
| 70 | 1 | vpk->block_count = (st->duration + (samples_per_block - 1)) / samples_per_block; | |
| 71 | 1 | vpk->last_block_size = (st->duration % samples_per_block) * 16 * st->codecpar->ch_layout.nb_channels / 28; | |
| 72 | |||
| 73 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
|
1 | if (offset < avio_tell(s->pb)) |
| 74 | ✗ | return AVERROR_INVALIDDATA; | |
| 75 | 1 | avio_skip(s->pb, offset - avio_tell(s->pb)); | |
| 76 | 1 | vpk->data_start = offset; | |
| 77 | 1 | avpriv_set_pts_info(st, 64, 1, st->codecpar->sample_rate); | |
| 78 | |||
| 79 | 1 | return 0; | |
| 80 | } | ||
| 81 | |||
| 82 | 2 | static int vpk_read_packet(AVFormatContext *s, AVPacket *pkt) | |
| 83 | { | ||
| 84 | 2 | AVCodecParameters *par = s->streams[0]->codecpar; | |
| 85 | 2 | VPKDemuxContext *vpk = s->priv_data; | |
| 86 | int ret, i; | ||
| 87 | |||
| 88 | 2 | vpk->current_block++; | |
| 89 |
2/2✓ Branch 0 taken 1 times.
✓ Branch 1 taken 1 times.
|
2 | if (vpk->current_block == vpk->block_count) { |
| 90 | unsigned size, skip; | ||
| 91 | uint64_t pos; | ||
| 92 | |||
| 93 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
|
1 | if (par->ch_layout.nb_channels <= 0) |
| 94 | ✗ | return AVERROR_INVALIDDATA; | |
| 95 | 1 | size = vpk->last_block_size / par->ch_layout.nb_channels; | |
| 96 | 1 | skip = (par->block_align - vpk->last_block_size) / par->ch_layout.nb_channels; | |
| 97 | 1 | pos = avio_tell(s->pb); | |
| 98 | |||
| 99 | 1 | ret = av_new_packet(pkt, vpk->last_block_size); | |
| 100 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
|
1 | if (ret < 0) |
| 101 | ✗ | return ret; | |
| 102 |
1/2✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
|
1 | for (i = 0; i < par->ch_layout.nb_channels; i++) { |
| 103 | 1 | ret = ffio_read_size(s->pb, pkt->data + i * size, size); | |
| 104 | 1 | avio_skip(s->pb, skip); | |
| 105 |
1/2✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
|
1 | if (ret < 0) { |
| 106 | 1 | return ret; | |
| 107 | } | ||
| 108 | } | ||
| 109 | ✗ | pkt->pos = pos; | |
| 110 | ✗ | pkt->stream_index = 0; | |
| 111 |
1/2✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
|
1 | } else if (vpk->current_block < vpk->block_count) { |
| 112 | 1 | ret = av_get_packet(s->pb, pkt, par->block_align); | |
| 113 | 1 | pkt->stream_index = 0; | |
| 114 | } else { | ||
| 115 | ✗ | return AVERROR_EOF; | |
| 116 | } | ||
| 117 | |||
| 118 | 1 | return ret; | |
| 119 | } | ||
| 120 | |||
| 121 | ✗ | static int vpk_read_seek(AVFormatContext *s, int stream_index, | |
| 122 | int64_t timestamp, int flags) | ||
| 123 | { | ||
| 124 | ✗ | AVStream *st = s->streams[stream_index]; | |
| 125 | ✗ | AVCodecParameters *par = st->codecpar; | |
| 126 | ✗ | VPKDemuxContext *vpk = s->priv_data; | |
| 127 | int samples_per_block; | ||
| 128 | ✗ | int64_t ret = 0; | |
| 129 | |||
| 130 | ✗ | samples_per_block = av_get_audio_frame_duration2(par, par->block_align); | |
| 131 | ✗ | if (samples_per_block > 0) | |
| 132 | ✗ | timestamp /= samples_per_block; | |
| 133 | else | ||
| 134 | ✗ | return -1; | |
| 135 | ✗ | ret = avio_seek(s->pb, vpk->data_start + timestamp * par->block_align, SEEK_SET); | |
| 136 | ✗ | if (ret < 0) | |
| 137 | ✗ | return ret; | |
| 138 | |||
| 139 | ✗ | vpk->current_block = timestamp; | |
| 140 | ✗ | avpriv_update_cur_dts(s, st, timestamp * samples_per_block); | |
| 141 | ✗ | return 0; | |
| 142 | } | ||
| 143 | |||
| 144 | const FFInputFormat ff_vpk_demuxer = { | ||
| 145 | .p.name = "vpk", | ||
| 146 | .p.long_name = NULL_IF_CONFIG_SMALL("Sony PS2 VPK"), | ||
| 147 | .p.extensions = "vpk", | ||
| 148 | .priv_data_size = sizeof(VPKDemuxContext), | ||
| 149 | .read_probe = vpk_probe, | ||
| 150 | .read_header = vpk_read_header, | ||
| 151 | .read_packet = vpk_read_packet, | ||
| 152 | .read_seek = vpk_read_seek, | ||
| 153 | }; | ||
| 154 |