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.

362 lines
12KB

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