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