rpl.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360
  1. /*
  2. * ARMovie/RPL demuxer
  3. * Copyright (c) 2007 Christian Ohm, 2008 Eli Friedman
  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. #include "libavutil/avstring.h"
  22. #include "libavutil/dict.h"
  23. #include "avformat.h"
  24. #include <stdlib.h>
  25. #define RPL_SIGNATURE "ARMovie\x0A"
  26. #define RPL_SIGNATURE_SIZE 8
  27. /** 256 is arbitrary, but should be big enough for any reasonable file. */
  28. #define RPL_LINE_LENGTH 256
  29. static int rpl_probe(AVProbeData *p)
  30. {
  31. if (memcmp(p->buf, RPL_SIGNATURE, RPL_SIGNATURE_SIZE))
  32. return 0;
  33. return AVPROBE_SCORE_MAX;
  34. }
  35. typedef struct RPLContext {
  36. // RPL header data
  37. int32_t frames_per_chunk;
  38. // Stream position data
  39. uint32_t chunk_number;
  40. uint32_t chunk_part;
  41. uint32_t frame_in_part;
  42. } RPLContext;
  43. static int read_line(AVIOContext * pb, char* line, int bufsize)
  44. {
  45. int i;
  46. for (i = 0; i < bufsize - 1; i++) {
  47. int b = avio_r8(pb);
  48. if (b == 0)
  49. break;
  50. if (b == '\n') {
  51. line[i] = '\0';
  52. return 0;
  53. }
  54. line[i] = b;
  55. }
  56. line[i] = '\0';
  57. return -1;
  58. }
  59. static int32_t read_int(const char* line, const char** endptr, int* error)
  60. {
  61. unsigned long result = 0;
  62. for (; *line>='0' && *line<='9'; line++) {
  63. if (result > (0x7FFFFFFF - 9) / 10)
  64. *error = -1;
  65. result = 10 * result + *line - '0';
  66. }
  67. *endptr = line;
  68. return result;
  69. }
  70. static int32_t read_line_and_int(AVIOContext * pb, int* error)
  71. {
  72. char line[RPL_LINE_LENGTH];
  73. const char *endptr;
  74. *error |= read_line(pb, line, sizeof(line));
  75. return read_int(line, &endptr, error);
  76. }
  77. /** Parsing for fps, which can be a fraction. Unfortunately,
  78. * the spec for the header leaves out a lot of details,
  79. * so this is mostly guessing.
  80. */
  81. static AVRational read_fps(const char* line, int* error)
  82. {
  83. int64_t num, den = 1;
  84. AVRational result;
  85. num = read_int(line, &line, error);
  86. if (*line == '.')
  87. line++;
  88. for (; *line>='0' && *line<='9'; line++) {
  89. // Truncate any numerator too large to fit into an int64_t
  90. if (num > (INT64_MAX - 9) / 10 || den > INT64_MAX / 10)
  91. break;
  92. num = 10 * num + *line - '0';
  93. den *= 10;
  94. }
  95. if (!num)
  96. *error = -1;
  97. av_reduce(&result.num, &result.den, num, den, 0x7FFFFFFF);
  98. return result;
  99. }
  100. static int rpl_read_header(AVFormatContext *s, AVFormatParameters *ap)
  101. {
  102. AVIOContext *pb = s->pb;
  103. RPLContext *rpl = s->priv_data;
  104. AVStream *vst = NULL, *ast = NULL;
  105. int total_audio_size;
  106. int error = 0;
  107. uint32_t i;
  108. int32_t audio_format, chunk_catalog_offset, number_of_chunks;
  109. AVRational fps;
  110. char line[RPL_LINE_LENGTH];
  111. // The header for RPL/ARMovie files is 21 lines of text
  112. // containing the various header fields. The fields are always
  113. // in the same order, and other text besides the first
  114. // number usually isn't important.
  115. // (The spec says that there exists some significance
  116. // for the text in a few cases; samples needed.)
  117. error |= read_line(pb, line, sizeof(line)); // ARMovie
  118. error |= read_line(pb, line, sizeof(line)); // movie name
  119. av_dict_set(&s->metadata, "title" , line, 0);
  120. error |= read_line(pb, line, sizeof(line)); // date/copyright
  121. av_dict_set(&s->metadata, "copyright", line, 0);
  122. error |= read_line(pb, line, sizeof(line)); // author and other
  123. av_dict_set(&s->metadata, "author" , line, 0);
  124. // video headers
  125. vst = av_new_stream(s, 0);
  126. if (!vst)
  127. return AVERROR(ENOMEM);
  128. vst->codec->codec_type = AVMEDIA_TYPE_VIDEO;
  129. vst->codec->codec_tag = read_line_and_int(pb, &error); // video format
  130. vst->codec->width = read_line_and_int(pb, &error); // video width
  131. vst->codec->height = read_line_and_int(pb, &error); // video height
  132. vst->codec->bits_per_coded_sample = read_line_and_int(pb, &error); // video bits per sample
  133. error |= read_line(pb, line, sizeof(line)); // video frames per second
  134. fps = read_fps(line, &error);
  135. av_set_pts_info(vst, 32, fps.den, fps.num);
  136. // Figure out the video codec
  137. switch (vst->codec->codec_tag) {
  138. #if 0
  139. case 122:
  140. vst->codec->codec_id = CODEC_ID_ESCAPE122;
  141. break;
  142. #endif
  143. case 124:
  144. vst->codec->codec_id = CODEC_ID_ESCAPE124;
  145. // The header is wrong here, at least sometimes
  146. vst->codec->bits_per_coded_sample = 16;
  147. break;
  148. #if 0
  149. case 130:
  150. vst->codec->codec_id = CODEC_ID_ESCAPE130;
  151. break;
  152. #endif
  153. default:
  154. av_log(s, AV_LOG_WARNING,
  155. "RPL video format %i not supported yet!\n",
  156. vst->codec->codec_tag);
  157. vst->codec->codec_id = CODEC_ID_NONE;
  158. }
  159. // Audio headers
  160. // ARMovie supports multiple audio tracks; I don't have any
  161. // samples, though. This code will ignore additional tracks.
  162. audio_format = read_line_and_int(pb, &error); // audio format ID
  163. if (audio_format) {
  164. ast = av_new_stream(s, 0);
  165. if (!ast)
  166. return AVERROR(ENOMEM);
  167. ast->codec->codec_type = AVMEDIA_TYPE_AUDIO;
  168. ast->codec->codec_tag = audio_format;
  169. ast->codec->sample_rate = read_line_and_int(pb, &error); // audio bitrate
  170. ast->codec->channels = read_line_and_int(pb, &error); // number of audio channels
  171. ast->codec->bits_per_coded_sample = read_line_and_int(pb, &error); // audio bits per sample
  172. // At least one sample uses 0 for ADPCM, which is really 4 bits
  173. // per sample.
  174. if (ast->codec->bits_per_coded_sample == 0)
  175. ast->codec->bits_per_coded_sample = 4;
  176. ast->codec->bit_rate = ast->codec->sample_rate *
  177. ast->codec->bits_per_coded_sample *
  178. ast->codec->channels;
  179. ast->codec->codec_id = CODEC_ID_NONE;
  180. switch (audio_format) {
  181. case 1:
  182. if (ast->codec->bits_per_coded_sample == 16) {
  183. // 16-bit audio is always signed
  184. ast->codec->codec_id = CODEC_ID_PCM_S16LE;
  185. break;
  186. }
  187. // There are some other formats listed as legal per the spec;
  188. // samples needed.
  189. break;
  190. case 101:
  191. if (ast->codec->bits_per_coded_sample == 8) {
  192. // The samples with this kind of audio that I have
  193. // are all unsigned.
  194. ast->codec->codec_id = CODEC_ID_PCM_U8;
  195. break;
  196. } else if (ast->codec->bits_per_coded_sample == 4) {
  197. ast->codec->codec_id = CODEC_ID_ADPCM_IMA_EA_SEAD;
  198. break;
  199. }
  200. break;
  201. }
  202. if (ast->codec->codec_id == CODEC_ID_NONE) {
  203. av_log(s, AV_LOG_WARNING,
  204. "RPL audio format %i not supported yet!\n",
  205. audio_format);
  206. }
  207. av_set_pts_info(ast, 32, 1, ast->codec->bit_rate);
  208. } else {
  209. for (i = 0; i < 3; i++)
  210. error |= read_line(pb, line, sizeof(line));
  211. }
  212. rpl->frames_per_chunk = read_line_and_int(pb, &error); // video frames per chunk
  213. if (rpl->frames_per_chunk > 1 && vst->codec->codec_tag != 124)
  214. av_log(s, AV_LOG_WARNING,
  215. "Don't know how to split frames for video format %i. "
  216. "Video stream will be broken!\n", vst->codec->codec_tag);
  217. number_of_chunks = read_line_and_int(pb, &error); // number of chunks in the file
  218. // The number in the header is actually the index of the last chunk.
  219. number_of_chunks++;
  220. error |= read_line(pb, line, sizeof(line)); // "even" chunk size in bytes
  221. error |= read_line(pb, line, sizeof(line)); // "odd" chunk size in bytes
  222. chunk_catalog_offset = // offset of the "chunk catalog"
  223. read_line_and_int(pb, &error); // (file index)
  224. error |= read_line(pb, line, sizeof(line)); // offset to "helpful" sprite
  225. error |= read_line(pb, line, sizeof(line)); // size of "helpful" sprite
  226. error |= read_line(pb, line, sizeof(line)); // offset to key frame list
  227. // Read the index
  228. avio_seek(pb, chunk_catalog_offset, SEEK_SET);
  229. total_audio_size = 0;
  230. for (i = 0; i < number_of_chunks; i++) {
  231. int64_t offset, video_size, audio_size;
  232. error |= read_line(pb, line, sizeof(line));
  233. if (3 != sscanf(line, "%"PRId64" , %"PRId64" ; %"PRId64,
  234. &offset, &video_size, &audio_size))
  235. error = -1;
  236. av_add_index_entry(vst, offset, i * rpl->frames_per_chunk,
  237. video_size, rpl->frames_per_chunk, 0);
  238. if (ast)
  239. av_add_index_entry(ast, offset + video_size, total_audio_size,
  240. audio_size, audio_size * 8, 0);
  241. total_audio_size += audio_size * 8;
  242. }
  243. if (error) return AVERROR(EIO);
  244. return 0;
  245. }
  246. static int rpl_read_packet(AVFormatContext *s, AVPacket *pkt)
  247. {
  248. RPLContext *rpl = s->priv_data;
  249. AVIOContext *pb = s->pb;
  250. AVStream* stream;
  251. AVIndexEntry* index_entry;
  252. uint32_t ret;
  253. if (rpl->chunk_part == s->nb_streams) {
  254. rpl->chunk_number++;
  255. rpl->chunk_part = 0;
  256. }
  257. stream = s->streams[rpl->chunk_part];
  258. if (rpl->chunk_number >= stream->nb_index_entries)
  259. return -1;
  260. index_entry = &stream->index_entries[rpl->chunk_number];
  261. if (rpl->frame_in_part == 0)
  262. if (avio_seek(pb, index_entry->pos, SEEK_SET) < 0)
  263. return AVERROR(EIO);
  264. if (stream->codec->codec_type == AVMEDIA_TYPE_VIDEO &&
  265. stream->codec->codec_tag == 124) {
  266. // We have to split Escape 124 frames because there are
  267. // multiple frames per chunk in Escape 124 samples.
  268. uint32_t frame_size;
  269. avio_skip(pb, 4); /* flags */
  270. frame_size = avio_rl32(pb);
  271. if (avio_seek(pb, -8, SEEK_CUR) < 0)
  272. return AVERROR(EIO);
  273. ret = av_get_packet(pb, pkt, frame_size);
  274. if (ret != frame_size) {
  275. av_free_packet(pkt);
  276. return AVERROR(EIO);
  277. }
  278. pkt->duration = 1;
  279. pkt->pts = index_entry->timestamp + rpl->frame_in_part;
  280. pkt->stream_index = rpl->chunk_part;
  281. rpl->frame_in_part++;
  282. if (rpl->frame_in_part == rpl->frames_per_chunk) {
  283. rpl->frame_in_part = 0;
  284. rpl->chunk_part++;
  285. }
  286. } else {
  287. ret = av_get_packet(pb, pkt, index_entry->size);
  288. if (ret != index_entry->size) {
  289. av_free_packet(pkt);
  290. return AVERROR(EIO);
  291. }
  292. if (stream->codec->codec_type == AVMEDIA_TYPE_VIDEO) {
  293. // frames_per_chunk should always be one here; the header
  294. // parsing will warn if it isn't.
  295. pkt->duration = rpl->frames_per_chunk;
  296. } else {
  297. // All the audio codecs supported in this container
  298. // (at least so far) are constant-bitrate.
  299. pkt->duration = ret * 8;
  300. }
  301. pkt->pts = index_entry->timestamp;
  302. pkt->stream_index = rpl->chunk_part;
  303. rpl->chunk_part++;
  304. }
  305. // None of the Escape formats have keyframes, and the ADPCM
  306. // format used doesn't have keyframes.
  307. if (rpl->chunk_number == 0 && rpl->frame_in_part == 0)
  308. pkt->flags |= AV_PKT_FLAG_KEY;
  309. return ret;
  310. }
  311. AVInputFormat ff_rpl_demuxer = {
  312. .name = "rpl",
  313. .long_name = NULL_IF_CONFIG_SMALL("RPL/ARMovie format"),
  314. .priv_data_size = sizeof(RPLContext),
  315. .read_probe = rpl_probe,
  316. .read_header = rpl_read_header,
  317. .read_packet = rpl_read_packet,
  318. };