You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

480 lines
14KB

  1. /*
  2. * Digital Pictures SGA game demuxer
  3. *
  4. * Copyright (C) 2021 Paul B Mahol
  5. *
  6. * This file is part of FFmpeg.
  7. *
  8. * FFmpeg is free software; you can redistribute it and/or
  9. * modify it under the terms of the GNU Lesser General Public
  10. * License as published by the Free Software Foundation; either
  11. * version 2.1 of the License, or (at your option) any later version.
  12. *
  13. * FFmpeg is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  16. * Lesser General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU Lesser General Public
  19. * License along with FFmpeg; if not, write to the Free Software
  20. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  21. */
  22. #include "libavutil/intreadwrite.h"
  23. #include "libavutil/avassert.h"
  24. #include "libavutil/internal.h"
  25. #include "avformat.h"
  26. #include "internal.h"
  27. #include "avio_internal.h"
  28. #define SEGA_CD_PCM_NUM 12500000
  29. #define SEGA_CD_PCM_DEN 786432
  30. typedef struct SGADemuxContext {
  31. int video_stream_index;
  32. int audio_stream_index;
  33. uint8_t sector[65536 * 2];
  34. int sector_headers;
  35. int sample_rate;
  36. int first_audio_size;
  37. int payload_size;
  38. int packet_type;
  39. int flags;
  40. int idx;
  41. int left;
  42. int64_t pkt_pos;
  43. } SGADemuxContext;
  44. static int sga_probe(const AVProbeData *p)
  45. {
  46. const uint8_t *src = p->buf;
  47. int score = 0, sectors = 1;
  48. int last_left = 0;
  49. int sample_rate = -1;
  50. if (p->buf_size < 2048)
  51. return 0;
  52. for (int i = 0; i + 2 < p->buf_size; i += 2048) {
  53. int header = AV_RB16(src + i);
  54. if ((header > 0x07FE && header < 0x8100) ||
  55. (header > 0x8200 && header < 0xA100) ||
  56. (header > 0xA200 && header < 0xC100)) {
  57. sectors = 0;
  58. break;
  59. }
  60. }
  61. for (int i = 0; i + 4 < p->buf_size;) {
  62. int header = AV_RB16(src + i);
  63. int left = AV_RB16(src + i + 2);
  64. int offset, type, size;
  65. if (last_left < 0)
  66. return 0;
  67. if (sectors && header && last_left == 0) {
  68. if (header >> 12) {
  69. last_left = left;
  70. } else {
  71. last_left = left = header;
  72. }
  73. } else if (sectors && header) {
  74. left = header;
  75. last_left -= left;
  76. if (header != 0x7FE && left < 7)
  77. return 0;
  78. } else if (sectors) {
  79. if (left <= 8)
  80. return 0;
  81. i += sectors ? 2048 : left + 4;
  82. last_left = 0;
  83. continue;
  84. }
  85. if (sectors && (i > 0 && left < 0x7fe) &&
  86. (i + left + 14 < p->buf_size)) {
  87. offset = i + left + 2;
  88. } else if (sectors && i > 0) {
  89. i += 2048;
  90. last_left -= FFMIN(last_left, 2046);
  91. continue;
  92. } else {
  93. offset = 0;
  94. last_left = left;
  95. }
  96. header = AV_RB16(src + offset);
  97. size = AV_RB16(src + offset + 2) + 4;
  98. while ((header & 0xFF00) == 0) {
  99. offset++;
  100. if (offset + 4 >= p->buf_size)
  101. break;
  102. header = AV_RB16(src + offset);
  103. size = AV_RB16(src + offset + 2) + 4;
  104. }
  105. if (offset + 12 >= p->buf_size)
  106. break;
  107. if ((header & 0xFF) > 1)
  108. return 0;
  109. type = header >> 8;
  110. if (type == 0xAA ||
  111. type == 0xA1 ||
  112. type == 0xA2 ||
  113. type == 0xA3) {
  114. int new_rate;
  115. if (size <= 12)
  116. return 0;
  117. new_rate = AV_RB16(src + offset + 8);
  118. if (sample_rate < 0)
  119. sample_rate = new_rate;
  120. if (sample_rate == 0 || new_rate != sample_rate)
  121. return 0;
  122. if (src[offset + 10] != 1)
  123. return 0;
  124. score += 10;
  125. } else if (type == 0xC1 ||
  126. type == 0xC6 ||
  127. type == 0xC7 ||
  128. type == 0xC8 ||
  129. type == 0xC9 ||
  130. type == 0xCB ||
  131. type == 0xCD ||
  132. type == 0xE7) {
  133. int nb_pals = src[offset + 9];
  134. int tiles_w = src[offset + 10];
  135. int tiles_h = src[offset + 11];
  136. if (size <= 12)
  137. return 0;
  138. if (nb_pals == 0 || nb_pals > 4)
  139. return 0;
  140. if (tiles_w == 0 || tiles_w > 80)
  141. return 0;
  142. if (tiles_h == 0 || tiles_h > 60)
  143. return 0;
  144. score += 10;
  145. } else if (header == 0x7FE) {
  146. ;
  147. } else {
  148. return 0;
  149. }
  150. i += sectors ? 2048 : size + 4;
  151. last_left -= FFMIN(last_left, 2046);
  152. if (score < 0)
  153. break;
  154. }
  155. return av_clip(score, 0, AVPROBE_SCORE_MAX);
  156. }
  157. static int sga_read_header(AVFormatContext *s)
  158. {
  159. SGADemuxContext *sga = s->priv_data;
  160. AVIOContext *pb = s->pb;
  161. sga->sector_headers = 1;
  162. sga->first_audio_size = 0;
  163. sga->video_stream_index = -1;
  164. sga->audio_stream_index = -1;
  165. sga->left = 2048;
  166. sga->idx = 0;
  167. s->ctx_flags |= AVFMTCTX_NOHEADER;
  168. if (pb->seekable & AVIO_SEEKABLE_NORMAL) {
  169. while (!avio_feof(pb)) {
  170. int header = avio_rb16(pb);
  171. int type = header >> 8;
  172. int skip = 2046;
  173. int clock;
  174. if (!sga->first_audio_size &&
  175. (type == 0xAA ||
  176. type == 0xA1 ||
  177. type == 0xA2 ||
  178. type == 0xA3)) {
  179. sga->first_audio_size = avio_rb16(pb);
  180. avio_skip(pb, 4);
  181. clock = avio_rb16(pb);
  182. sga->sample_rate = av_rescale(clock,
  183. SEGA_CD_PCM_NUM,
  184. SEGA_CD_PCM_DEN);
  185. skip -= 8;
  186. }
  187. if ((header > 0x07FE && header < 0x8100) ||
  188. (header > 0x8200 && header < 0xA100) ||
  189. (header > 0xA200 && header < 0xC100)) {
  190. sga->sector_headers = 0;
  191. break;
  192. }
  193. avio_skip(pb, skip);
  194. }
  195. avio_seek(pb, 0, SEEK_SET);
  196. }
  197. return 0;
  198. }
  199. static void print_stats(AVFormatContext *s, const char *where)
  200. {
  201. SGADemuxContext *sga = s->priv_data;
  202. av_log(s, AV_LOG_DEBUG, "START %s\n", where);
  203. av_log(s, AV_LOG_DEBUG, "pos: %"PRIX64"\n", avio_tell(s->pb));
  204. av_log(s, AV_LOG_DEBUG, "idx: %X\n", sga->idx);
  205. av_log(s, AV_LOG_DEBUG, "packet_type: %X\n", sga->packet_type);
  206. av_log(s, AV_LOG_DEBUG, "payload_size: %X\n", sga->payload_size);
  207. av_log(s, AV_LOG_DEBUG, "SECTOR: %016"PRIX64"\n", AV_RB64(sga->sector));
  208. av_log(s, AV_LOG_DEBUG, "stream: %X\n", sga->sector[1]);
  209. av_log(s, AV_LOG_DEBUG, "END %s\n", where);
  210. }
  211. static void update_type_size(AVFormatContext *s)
  212. {
  213. SGADemuxContext *sga = s->priv_data;
  214. if (sga->idx >= 4) {
  215. sga->packet_type = sga->sector[0];
  216. sga->payload_size = AV_RB16(sga->sector + 2);
  217. } else {
  218. sga->packet_type = 0;
  219. sga->payload_size = 0;
  220. }
  221. }
  222. static int sga_video_packet(AVFormatContext *s, AVPacket *pkt)
  223. {
  224. SGADemuxContext *sga = s->priv_data;
  225. int ret;
  226. if (sga->payload_size <= 8)
  227. return AVERROR_INVALIDDATA;
  228. if (sga->video_stream_index == -1) {
  229. AVRational frame_rate;
  230. AVStream *st = avformat_new_stream(s, NULL);
  231. if (!st)
  232. return AVERROR(ENOMEM);
  233. st->start_time = 0;
  234. st->codecpar->codec_type = AVMEDIA_TYPE_VIDEO;
  235. st->codecpar->codec_tag = 0;
  236. st->codecpar->codec_id = AV_CODEC_ID_SGA_VIDEO;
  237. sga->video_stream_index = st->index;
  238. if (sga->first_audio_size > 0 && sga->sample_rate > 0) {
  239. frame_rate.num = sga->sample_rate;
  240. frame_rate.den = sga->first_audio_size;
  241. } else {
  242. frame_rate.num = 15;
  243. frame_rate.den = 1;
  244. }
  245. avpriv_set_pts_info(st, 64, frame_rate.den, frame_rate.num);
  246. }
  247. ret = av_new_packet(pkt, sga->payload_size + 4);
  248. if (ret < 0)
  249. return AVERROR(ENOMEM);
  250. memcpy(pkt->data, sga->sector, sga->payload_size + 4);
  251. av_assert0(sga->idx >= sga->payload_size + 4);
  252. memmove(sga->sector, sga->sector + sga->payload_size + 4, sga->idx - sga->payload_size - 4);
  253. pkt->stream_index = sga->video_stream_index;
  254. pkt->duration = 1;
  255. pkt->pos = sga->pkt_pos;
  256. pkt->flags |= sga->flags;
  257. sga->idx -= sga->payload_size + 4;
  258. sga->flags = 0;
  259. update_type_size(s);
  260. av_log(s, AV_LOG_DEBUG, "VIDEO PACKET: %d:%016"PRIX64" i:%X\n", pkt->size, AV_RB64(sga->sector), sga->idx);
  261. return 0;
  262. }
  263. static int sga_audio_packet(AVFormatContext *s, AVPacket *pkt)
  264. {
  265. SGADemuxContext *sga = s->priv_data;
  266. int ret;
  267. if (sga->payload_size <= 8)
  268. return AVERROR_INVALIDDATA;
  269. if (sga->audio_stream_index == -1) {
  270. AVStream *st = avformat_new_stream(s, NULL);
  271. if (!st)
  272. return AVERROR(ENOMEM);
  273. st->start_time = 0;
  274. st->codecpar->codec_type = AVMEDIA_TYPE_AUDIO;
  275. st->codecpar->codec_tag = 0;
  276. st->codecpar->codec_id = AV_CODEC_ID_PCM_SGA;
  277. st->codecpar->channels = 1;
  278. st->codecpar->channel_layout= AV_CH_LAYOUT_MONO;
  279. st->codecpar->sample_rate = av_rescale(AV_RB16(sga->sector + 8),
  280. SEGA_CD_PCM_NUM,
  281. SEGA_CD_PCM_DEN);
  282. sga->audio_stream_index = st->index;
  283. avpriv_set_pts_info(st, 64, 1, st->codecpar->sample_rate);
  284. }
  285. ret = av_new_packet(pkt, sga->payload_size - 8);
  286. if (ret < 0)
  287. return AVERROR(ENOMEM);
  288. memcpy(pkt->data, sga->sector + 12, sga->payload_size - 8);
  289. av_assert0(sga->idx >= sga->payload_size + 4);
  290. memmove(sga->sector, sga->sector + sga->payload_size + 4, sga->idx - sga->payload_size - 4);
  291. pkt->stream_index = sga->audio_stream_index;
  292. pkt->duration = pkt->size;
  293. pkt->pos = sga->pkt_pos;
  294. pkt->flags |= sga->flags;
  295. sga->idx -= sga->payload_size + 4;
  296. sga->flags = 0;
  297. update_type_size(s);
  298. av_log(s, AV_LOG_DEBUG, "AUDIO PACKET: %d:%016"PRIX64" i:%X\n", pkt->size, AV_RB64(sga->sector), sga->idx);
  299. return 0;
  300. }
  301. static int sga_packet(AVFormatContext *s, AVPacket *pkt)
  302. {
  303. SGADemuxContext *sga = s->priv_data;
  304. int ret = 0;
  305. if (sga->packet_type == 0xCD ||
  306. sga->packet_type == 0xCB ||
  307. sga->packet_type == 0xC9 ||
  308. sga->packet_type == 0xC8 ||
  309. sga->packet_type == 0xC7 ||
  310. sga->packet_type == 0xC6 ||
  311. sga->packet_type == 0xC1 ||
  312. sga->packet_type == 0xE7) {
  313. ret = sga_video_packet(s, pkt);
  314. } else if (sga->packet_type == 0xA1 ||
  315. sga->packet_type == 0xA2 ||
  316. sga->packet_type == 0xA3 ||
  317. sga->packet_type == 0xAA) {
  318. ret = sga_audio_packet(s, pkt);
  319. } else {
  320. if (sga->idx == 0)
  321. return AVERROR_EOF;
  322. if (sga->sector[0])
  323. return AVERROR_INVALIDDATA;
  324. memmove(sga->sector, sga->sector + 1, sga->idx - 1);
  325. sga->idx--;
  326. return AVERROR(EAGAIN);
  327. }
  328. return ret;
  329. }
  330. static int try_packet(AVFormatContext *s, AVPacket *pkt)
  331. {
  332. SGADemuxContext *sga = s->priv_data;
  333. int ret = AVERROR(EAGAIN);
  334. update_type_size(s);
  335. if (sga->idx >= sga->payload_size + 4) {
  336. print_stats(s, "before sga_packet");
  337. ret = sga_packet(s, pkt);
  338. print_stats(s, "after sga_packet");
  339. if (ret != AVERROR(EAGAIN))
  340. return ret;
  341. }
  342. return sga->idx < sga->payload_size + 4 ? AVERROR(EAGAIN) : ret;
  343. }
  344. static int sga_read_packet(AVFormatContext *s, AVPacket *pkt)
  345. {
  346. SGADemuxContext *sga = s->priv_data;
  347. AVIOContext *pb = s->pb;
  348. int header, ret = 0;
  349. sga->pkt_pos = avio_tell(pb);
  350. retry:
  351. update_type_size(s);
  352. print_stats(s, "start");
  353. if (avio_feof(pb) &&
  354. (!sga->payload_size || sga->idx < sga->payload_size + 4))
  355. return AVERROR_EOF;
  356. if (sga->idx < sga->payload_size + 4) {
  357. ret = ffio_ensure_seekback(pb, 2);
  358. if (ret < 0)
  359. return ret;
  360. print_stats(s, "before read header");
  361. header = avio_rb16(pb);
  362. if (!header) {
  363. avio_skip(pb, 2046);
  364. sga->left = 0;
  365. } else if (!avio_feof(pb) &&
  366. ((header >> 15) ||
  367. !sga->sector_headers)) {
  368. avio_seek(pb, -2, SEEK_CUR);
  369. sga->flags = AV_PKT_FLAG_KEY;
  370. sga->left = 2048;
  371. } else {
  372. sga->left = 2046;
  373. }
  374. av_assert0(sga->idx + sga->left < sizeof(sga->sector));
  375. ret = avio_read(pb, sga->sector + sga->idx, sga->left);
  376. if (ret > 0)
  377. sga->idx += ret;
  378. else if (ret != AVERROR_EOF && ret)
  379. return ret;
  380. print_stats(s, "after read header");
  381. update_type_size(s);
  382. }
  383. ret = try_packet(s, pkt);
  384. if (ret == AVERROR(EAGAIN))
  385. goto retry;
  386. return ret;
  387. }
  388. static int sga_seek(AVFormatContext *s, int stream_index,
  389. int64_t timestamp, int flags)
  390. {
  391. SGADemuxContext *sga = s->priv_data;
  392. sga->packet_type = sga->payload_size = sga->idx = 0;
  393. memset(sga->sector, 0, sizeof(sga->sector));
  394. return -1;
  395. }
  396. AVInputFormat ff_sga_demuxer = {
  397. .name = "sga",
  398. .long_name = NULL_IF_CONFIG_SMALL("Digital Pictures SGA"),
  399. .priv_data_size = sizeof(SGADemuxContext),
  400. .read_probe = sga_probe,
  401. .read_header = sga_read_header,
  402. .read_packet = sga_read_packet,
  403. .read_seek = sga_seek,
  404. .extensions = "sga",
  405. .flags = AVFMT_GENERIC_INDEX,
  406. };