rpl.c 12 KB

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