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 <inttypes.h>
  22. #include <stdlib.h>
  23. #include "libavutil/avstring.h"
  24. #include "libavutil/dict.h"
  25. #include "avformat.h"
  26. #include "internal.h"
  27. #define RPL_SIGNATURE "ARMovie\x0A"
  28. #define RPL_SIGNATURE_SIZE 8
  29. /** 256 is arbitrary, but should be big enough for any reasonable file. */
  30. #define RPL_LINE_LENGTH 256
  31. static int rpl_probe(AVProbeData *p)
  32. {
  33. if (memcmp(p->buf, RPL_SIGNATURE, RPL_SIGNATURE_SIZE))
  34. return 0;
  35. return AVPROBE_SCORE_MAX;
  36. }
  37. typedef struct RPLContext {
  38. // RPL header data
  39. int32_t frames_per_chunk;
  40. // Stream position data
  41. uint32_t chunk_number;
  42. uint32_t chunk_part;
  43. uint32_t frame_in_part;
  44. } RPLContext;
  45. static int read_line(AVIOContext * pb, char* line, int bufsize)
  46. {
  47. int i;
  48. for (i = 0; i < bufsize - 1; i++) {
  49. int b = avio_r8(pb);
  50. if (b == 0)
  51. break;
  52. if (b == '\n') {
  53. line[i] = '\0';
  54. return 0;
  55. }
  56. line[i] = b;
  57. }
  58. line[i] = '\0';
  59. return -1;
  60. }
  61. static int32_t read_int(const char* line, const char** endptr, int* error)
  62. {
  63. unsigned long result = 0;
  64. for (; *line>='0' && *line<='9'; line++) {
  65. if (result > (0x7FFFFFFF - 9) / 10)
  66. *error = -1;
  67. result = 10 * result + *line - '0';
  68. }
  69. *endptr = line;
  70. return result;
  71. }
  72. static int32_t read_line_and_int(AVIOContext * pb, int* error)
  73. {
  74. char line[RPL_LINE_LENGTH];
  75. const char *endptr;
  76. *error |= read_line(pb, line, sizeof(line));
  77. return read_int(line, &endptr, error);
  78. }
  79. /** Parsing for fps, which can be a fraction. Unfortunately,
  80. * the spec for the header leaves out a lot of details,
  81. * so this is mostly guessing.
  82. */
  83. static AVRational read_fps(const char* line, int* error)
  84. {
  85. int64_t num, den = 1;
  86. AVRational result;
  87. num = read_int(line, &line, error);
  88. if (*line == '.')
  89. line++;
  90. for (; *line>='0' && *line<='9'; line++) {
  91. // Truncate any numerator too large to fit into an int64_t
  92. if (num > (INT64_MAX - 9) / 10 || den > INT64_MAX / 10)
  93. break;
  94. num = 10 * num + *line - '0';
  95. den *= 10;
  96. }
  97. if (!num)
  98. *error = -1;
  99. av_reduce(&result.num, &result.den, num, den, 0x7FFFFFFF);
  100. return result;
  101. }
  102. static int rpl_read_header(AVFormatContext *s)
  103. {
  104. AVIOContext *pb = s->pb;
  105. RPLContext *rpl = s->priv_data;
  106. AVStream *vst = NULL, *ast = NULL;
  107. int total_audio_size;
  108. int error = 0;
  109. uint32_t i;
  110. int32_t audio_format, chunk_catalog_offset, number_of_chunks;
  111. AVRational fps;
  112. char line[RPL_LINE_LENGTH];
  113. // The header for RPL/ARMovie files is 21 lines of text
  114. // containing the various header fields. The fields are always
  115. // in the same order, and other text besides the first
  116. // number usually isn't important.
  117. // (The spec says that there exists some significance
  118. // for the text in a few cases; samples needed.)
  119. error |= read_line(pb, line, sizeof(line)); // ARMovie
  120. error |= read_line(pb, line, sizeof(line)); // movie name
  121. av_dict_set(&s->metadata, "title" , line, 0);
  122. error |= read_line(pb, line, sizeof(line)); // date/copyright
  123. av_dict_set(&s->metadata, "copyright", line, 0);
  124. error |= read_line(pb, line, sizeof(line)); // author and other
  125. av_dict_set(&s->metadata, "author" , line, 0);
  126. // video headers
  127. vst = avformat_new_stream(s, NULL);
  128. if (!vst)
  129. return AVERROR(ENOMEM);
  130. vst->codecpar->codec_type = AVMEDIA_TYPE_VIDEO;
  131. vst->codecpar->codec_tag = read_line_and_int(pb, &error); // video format
  132. vst->codecpar->width = read_line_and_int(pb, &error); // video width
  133. vst->codecpar->height = read_line_and_int(pb, &error); // video height
  134. vst->codecpar->bits_per_coded_sample = read_line_and_int(pb, &error); // video bits per sample
  135. error |= read_line(pb, line, sizeof(line)); // video frames per second
  136. fps = read_fps(line, &error);
  137. avpriv_set_pts_info(vst, 32, fps.den, fps.num);
  138. // Figure out the video codec
  139. switch (vst->codecpar->codec_tag) {
  140. #if 0
  141. case 122:
  142. vst->codecpar->codec_id = AV_CODEC_ID_ESCAPE122;
  143. break;
  144. #endif
  145. case 124:
  146. vst->codecpar->codec_id = AV_CODEC_ID_ESCAPE124;
  147. // The header is wrong here, at least sometimes
  148. vst->codecpar->bits_per_coded_sample = 16;
  149. break;
  150. case 130:
  151. vst->codecpar->codec_id = AV_CODEC_ID_ESCAPE130;
  152. break;
  153. default:
  154. av_log(s, AV_LOG_WARNING,
  155. "RPL video format %"PRIu32" not supported yet!\n",
  156. vst->codecpar->codec_tag);
  157. vst->codecpar->codec_id = AV_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 = avformat_new_stream(s, NULL);
  165. if (!ast)
  166. return AVERROR(ENOMEM);
  167. ast->codecpar->codec_type = AVMEDIA_TYPE_AUDIO;
  168. ast->codecpar->codec_tag = audio_format;
  169. ast->codecpar->sample_rate = read_line_and_int(pb, &error); // audio bitrate
  170. ast->codecpar->channels = read_line_and_int(pb, &error); // number of audio channels
  171. ast->codecpar->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->codecpar->bits_per_coded_sample == 0)
  175. ast->codecpar->bits_per_coded_sample = 4;
  176. ast->codecpar->bit_rate = ast->codecpar->sample_rate *
  177. ast->codecpar->bits_per_coded_sample *
  178. ast->codecpar->channels;
  179. ast->codecpar->codec_id = AV_CODEC_ID_NONE;
  180. switch (audio_format) {
  181. case 1:
  182. if (ast->codecpar->bits_per_coded_sample == 16) {
  183. // 16-bit audio is always signed
  184. ast->codecpar->codec_id = AV_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->codecpar->bits_per_coded_sample == 8) {
  192. // The samples with this kind of audio that I have
  193. // are all unsigned.
  194. ast->codecpar->codec_id = AV_CODEC_ID_PCM_U8;
  195. break;
  196. } else if (ast->codecpar->bits_per_coded_sample == 4) {
  197. ast->codecpar->codec_id = AV_CODEC_ID_ADPCM_IMA_EA_SEAD;
  198. break;
  199. }
  200. break;
  201. }
  202. if (ast->codecpar->codec_id == AV_CODEC_ID_NONE) {
  203. av_log(s, AV_LOG_WARNING,
  204. "RPL audio format %"PRId32" not supported yet!\n",
  205. audio_format);
  206. }
  207. avpriv_set_pts_info(ast, 32, 1, ast->codecpar->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->codecpar->codec_tag != 124)
  214. av_log(s, AV_LOG_WARNING,
  215. "Don't know how to split frames for video format %"PRIu32". "
  216. "Video stream will be broken!\n", vst->codecpar->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->codecpar->codec_type == AVMEDIA_TYPE_VIDEO &&
  265. stream->codecpar->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_packet_unref(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_packet_unref(pkt);
  290. return AVERROR(EIO);
  291. }
  292. if (stream->codecpar->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"),
  314. .priv_data_size = sizeof(RPLContext),
  315. .read_probe = rpl_probe,
  316. .read_header = rpl_read_header,
  317. .read_packet = rpl_read_packet,
  318. };