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.

357 lines
12KB

  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, s->title , sizeof(s->title )); // movie name
  118. error |= read_line(pb, s->copyright, sizeof(s->copyright)); // date/copyright
  119. error |= read_line(pb, s->author , sizeof(s->author )); // author and other
  120. // video headers
  121. vst = av_new_stream(s, 0);
  122. if (!vst)
  123. return AVERROR(ENOMEM);
  124. vst->codec->codec_type = CODEC_TYPE_VIDEO;
  125. vst->codec->codec_tag = read_line_and_int(pb, &error); // video format
  126. vst->codec->width = read_line_and_int(pb, &error); // video width
  127. vst->codec->height = read_line_and_int(pb, &error); // video height
  128. vst->codec->bits_per_coded_sample = read_line_and_int(pb, &error); // video bits per sample
  129. error |= read_line(pb, line, sizeof(line)); // video frames per second
  130. fps = read_fps(line, &error);
  131. av_set_pts_info(vst, 32, fps.den, fps.num);
  132. // Figure out the video codec
  133. switch (vst->codec->codec_tag) {
  134. #if 0
  135. case 122:
  136. vst->codec->codec_id = CODEC_ID_ESCAPE122;
  137. break;
  138. #endif
  139. case 124:
  140. vst->codec->codec_id = CODEC_ID_ESCAPE124;
  141. // The header is wrong here, at least sometimes
  142. vst->codec->bits_per_coded_sample = 16;
  143. break;
  144. #if 0
  145. case 130:
  146. vst->codec->codec_id = CODEC_ID_ESCAPE130;
  147. break;
  148. #endif
  149. default:
  150. av_log(s, AV_LOG_WARNING,
  151. "RPL video format %i not supported yet!\n",
  152. vst->codec->codec_tag);
  153. vst->codec->codec_id = CODEC_ID_NONE;
  154. }
  155. // Audio headers
  156. // ARMovie supports multiple audio tracks; I don't have any
  157. // samples, though. This code will ignore additional tracks.
  158. audio_format = read_line_and_int(pb, &error); // audio format ID
  159. if (audio_format) {
  160. ast = av_new_stream(s, 0);
  161. if (!ast)
  162. return AVERROR(ENOMEM);
  163. ast->codec->codec_type = CODEC_TYPE_AUDIO;
  164. ast->codec->codec_tag = audio_format;
  165. ast->codec->sample_rate = read_line_and_int(pb, &error); // audio bitrate
  166. ast->codec->channels = read_line_and_int(pb, &error); // number of audio channels
  167. ast->codec->bits_per_coded_sample = read_line_and_int(pb, &error); // audio bits per sample
  168. // At least one sample uses 0 for ADPCM, which is really 4 bits
  169. // per sample.
  170. if (ast->codec->bits_per_coded_sample == 0)
  171. ast->codec->bits_per_coded_sample = 4;
  172. ast->codec->bit_rate = ast->codec->sample_rate *
  173. ast->codec->bits_per_coded_sample *
  174. ast->codec->channels;
  175. ast->codec->codec_id = CODEC_ID_NONE;
  176. switch (audio_format) {
  177. case 1:
  178. if (ast->codec->bits_per_coded_sample == 16) {
  179. // 16-bit audio is always signed
  180. ast->codec->codec_id = CODEC_ID_PCM_S16LE;
  181. break;
  182. }
  183. // There are some other formats listed as legal per the spec;
  184. // samples needed.
  185. break;
  186. case 101:
  187. if (ast->codec->bits_per_coded_sample == 8) {
  188. // The samples with this kind of audio that I have
  189. // are all unsigned.
  190. ast->codec->codec_id = CODEC_ID_PCM_U8;
  191. break;
  192. } else if (ast->codec->bits_per_coded_sample == 4) {
  193. ast->codec->codec_id = CODEC_ID_ADPCM_IMA_EA_SEAD;
  194. break;
  195. }
  196. break;
  197. }
  198. if (ast->codec->codec_id == CODEC_ID_NONE) {
  199. av_log(s, AV_LOG_WARNING,
  200. "RPL audio format %i not supported yet!\n",
  201. audio_format);
  202. }
  203. av_set_pts_info(ast, 32, 1, ast->codec->bit_rate);
  204. } else {
  205. for (i = 0; i < 3; i++)
  206. error |= read_line(pb, line, sizeof(line));
  207. }
  208. rpl->frames_per_chunk = read_line_and_int(pb, &error); // video frames per chunk
  209. if (rpl->frames_per_chunk > 1 && vst->codec->codec_tag != 124)
  210. av_log(s, AV_LOG_WARNING,
  211. "Don't know how to split frames for video format %i. "
  212. "Video stream will be broken!\n", vst->codec->codec_tag);
  213. number_of_chunks = read_line_and_int(pb, &error); // number of chunks in the file
  214. // The number in the header is actually the index of the last chunk.
  215. number_of_chunks++;
  216. error |= read_line(pb, line, sizeof(line)); // "even" chunk size in bytes
  217. error |= read_line(pb, line, sizeof(line)); // "odd" chunk size in bytes
  218. chunk_catalog_offset = // offset of the "chunk catalog"
  219. read_line_and_int(pb, &error); // (file index)
  220. error |= read_line(pb, line, sizeof(line)); // offset to "helpful" sprite
  221. error |= read_line(pb, line, sizeof(line)); // size of "helpful" sprite
  222. error |= read_line(pb, line, sizeof(line)); // offset to key frame list
  223. // Read the index
  224. url_fseek(pb, chunk_catalog_offset, SEEK_SET);
  225. total_audio_size = 0;
  226. for (i = 0; i < number_of_chunks; i++) {
  227. int64_t offset, video_size, audio_size;
  228. error |= read_line(pb, line, sizeof(line));
  229. if (3 != sscanf(line, "%"PRId64" , %"PRId64" ; %"PRId64,
  230. &offset, &video_size, &audio_size))
  231. error = -1;
  232. av_add_index_entry(vst, offset, i * rpl->frames_per_chunk,
  233. video_size, rpl->frames_per_chunk, 0);
  234. if (ast)
  235. av_add_index_entry(ast, offset + video_size, total_audio_size,
  236. audio_size, audio_size * 8, 0);
  237. total_audio_size += audio_size * 8;
  238. }
  239. if (error) return AVERROR(EIO);
  240. return 0;
  241. }
  242. static int rpl_read_packet(AVFormatContext *s, AVPacket *pkt)
  243. {
  244. RPLContext *rpl = s->priv_data;
  245. ByteIOContext *pb = s->pb;
  246. AVStream* stream;
  247. AVIndexEntry* index_entry;
  248. uint32_t ret;
  249. if (rpl->chunk_part == s->nb_streams) {
  250. rpl->chunk_number++;
  251. rpl->chunk_part = 0;
  252. }
  253. stream = s->streams[rpl->chunk_part];
  254. if (rpl->chunk_number >= stream->nb_index_entries)
  255. return -1;
  256. index_entry = &stream->index_entries[rpl->chunk_number];
  257. if (rpl->frame_in_part == 0)
  258. if (url_fseek(pb, index_entry->pos, SEEK_SET) < 0)
  259. return AVERROR(EIO);
  260. if (stream->codec->codec_type == CODEC_TYPE_VIDEO &&
  261. stream->codec->codec_tag == 124) {
  262. // We have to split Escape 124 frames because there are
  263. // multiple frames per chunk in Escape 124 samples.
  264. uint32_t frame_size, frame_flags;
  265. frame_flags = get_le32(pb);
  266. frame_size = get_le32(pb);
  267. if (url_fseek(pb, -8, SEEK_CUR) < 0)
  268. return AVERROR(EIO);
  269. ret = av_get_packet(pb, pkt, frame_size);
  270. if (ret != frame_size) {
  271. av_free_packet(pkt);
  272. return AVERROR(EIO);
  273. }
  274. pkt->duration = 1;
  275. pkt->pts = index_entry->timestamp + rpl->frame_in_part;
  276. pkt->stream_index = rpl->chunk_part;
  277. rpl->frame_in_part++;
  278. if (rpl->frame_in_part == rpl->frames_per_chunk) {
  279. rpl->frame_in_part = 0;
  280. rpl->chunk_part++;
  281. }
  282. } else {
  283. ret = av_get_packet(pb, pkt, index_entry->size);
  284. if (ret != index_entry->size) {
  285. av_free_packet(pkt);
  286. return AVERROR(EIO);
  287. }
  288. if (stream->codec->codec_type == CODEC_TYPE_VIDEO) {
  289. // frames_per_chunk should always be one here; the header
  290. // parsing will warn if it isn't.
  291. pkt->duration = rpl->frames_per_chunk;
  292. } else {
  293. // All the audio codecs supported in this container
  294. // (at least so far) are constant-bitrate.
  295. pkt->duration = ret * 8;
  296. }
  297. pkt->pts = index_entry->timestamp;
  298. pkt->stream_index = rpl->chunk_part;
  299. rpl->chunk_part++;
  300. }
  301. // None of the Escape formats have keyframes, and the ADPCM
  302. // format used doesn't have keyframes.
  303. if (rpl->chunk_number == 0 && rpl->frame_in_part == 0)
  304. pkt->flags |= PKT_FLAG_KEY;
  305. return ret;
  306. }
  307. AVInputFormat rpl_demuxer = {
  308. "rpl",
  309. NULL_IF_CONFIG_SMALL("RPL/ARMovie format"),
  310. sizeof(RPLContext),
  311. rpl_probe,
  312. rpl_read_header,
  313. rpl_read_packet,
  314. };