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.

360 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 <stdint.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 url_feof(pb) ? -1 : 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->codec->codec_type = AVMEDIA_TYPE_VIDEO;
  131. vst->codec->codec_tag = read_line_and_int(pb, &error); // video format
  132. vst->codec->width = read_line_and_int(pb, &error); // video width
  133. vst->codec->height = read_line_and_int(pb, &error); // video height
  134. vst->codec->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->codec->codec_tag) {
  140. #if 0
  141. case 122:
  142. vst->codec->codec_id = AV_CODEC_ID_ESCAPE122;
  143. break;
  144. #endif
  145. case 124:
  146. vst->codec->codec_id = AV_CODEC_ID_ESCAPE124;
  147. // The header is wrong here, at least sometimes
  148. vst->codec->bits_per_coded_sample = 16;
  149. break;
  150. case 130:
  151. vst->codec->codec_id = AV_CODEC_ID_ESCAPE130;
  152. break;
  153. default:
  154. avpriv_report_missing_feature(s, "Video format %i",
  155. vst->codec->codec_tag);
  156. vst->codec->codec_id = AV_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 = avformat_new_stream(s, NULL);
  164. if (!ast)
  165. return AVERROR(ENOMEM);
  166. ast->codec->codec_type = AVMEDIA_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 = AV_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 = AV_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 = AV_CODEC_ID_PCM_U8;
  194. break;
  195. } else if (ast->codec->bits_per_coded_sample == 4) {
  196. ast->codec->codec_id = AV_CODEC_ID_ADPCM_IMA_EA_SEAD;
  197. break;
  198. }
  199. break;
  200. }
  201. if (ast->codec->codec_id == AV_CODEC_ID_NONE)
  202. avpriv_request_sample(s, "Audio format %i", audio_format);
  203. avpriv_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. avio_seek(pb, chunk_catalog_offset, SEEK_SET);
  225. total_audio_size = 0;
  226. for (i = 0; !error && 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, "%"SCNd64" , %"SCNd64" ; %"SCNd64,
  230. &offset, &video_size, &audio_size)) {
  231. error = -1;
  232. continue;
  233. }
  234. av_add_index_entry(vst, offset, i * rpl->frames_per_chunk,
  235. video_size, rpl->frames_per_chunk, 0);
  236. if (ast)
  237. av_add_index_entry(ast, offset + video_size, total_audio_size,
  238. audio_size, audio_size * 8, 0);
  239. total_audio_size += audio_size * 8;
  240. }
  241. if (error) return AVERROR(EIO);
  242. return 0;
  243. }
  244. static int rpl_read_packet(AVFormatContext *s, AVPacket *pkt)
  245. {
  246. RPLContext *rpl = s->priv_data;
  247. AVIOContext *pb = s->pb;
  248. AVStream* stream;
  249. AVIndexEntry* index_entry;
  250. uint32_t ret;
  251. if (rpl->chunk_part == s->nb_streams) {
  252. rpl->chunk_number++;
  253. rpl->chunk_part = 0;
  254. }
  255. stream = s->streams[rpl->chunk_part];
  256. if (rpl->chunk_number >= stream->nb_index_entries)
  257. return AVERROR_EOF;
  258. index_entry = &stream->index_entries[rpl->chunk_number];
  259. if (rpl->frame_in_part == 0)
  260. if (avio_seek(pb, index_entry->pos, SEEK_SET) < 0)
  261. return AVERROR(EIO);
  262. if (stream->codec->codec_type == AVMEDIA_TYPE_VIDEO &&
  263. stream->codec->codec_tag == 124) {
  264. // We have to split Escape 124 frames because there are
  265. // multiple frames per chunk in Escape 124 samples.
  266. uint32_t frame_size;
  267. avio_skip(pb, 4); /* flags */
  268. frame_size = avio_rl32(pb);
  269. if (avio_seek(pb, -8, SEEK_CUR) < 0)
  270. return AVERROR(EIO);
  271. ret = av_get_packet(pb, pkt, frame_size);
  272. if (ret != frame_size) {
  273. av_free_packet(pkt);
  274. return AVERROR(EIO);
  275. }
  276. pkt->duration = 1;
  277. pkt->pts = index_entry->timestamp + rpl->frame_in_part;
  278. pkt->stream_index = rpl->chunk_part;
  279. rpl->frame_in_part++;
  280. if (rpl->frame_in_part == rpl->frames_per_chunk) {
  281. rpl->frame_in_part = 0;
  282. rpl->chunk_part++;
  283. }
  284. } else {
  285. ret = av_get_packet(pb, pkt, index_entry->size);
  286. if (ret != index_entry->size) {
  287. av_free_packet(pkt);
  288. return AVERROR(EIO);
  289. }
  290. if (stream->codec->codec_type == AVMEDIA_TYPE_VIDEO) {
  291. // frames_per_chunk should always be one here; the header
  292. // parsing will warn if it isn't.
  293. pkt->duration = rpl->frames_per_chunk;
  294. } else {
  295. // All the audio codecs supported in this container
  296. // (at least so far) are constant-bitrate.
  297. pkt->duration = ret * 8;
  298. }
  299. pkt->pts = index_entry->timestamp;
  300. pkt->stream_index = rpl->chunk_part;
  301. rpl->chunk_part++;
  302. }
  303. // None of the Escape formats have keyframes, and the ADPCM
  304. // format used doesn't have keyframes.
  305. if (rpl->chunk_number == 0 && rpl->frame_in_part == 0)
  306. pkt->flags |= AV_PKT_FLAG_KEY;
  307. return ret;
  308. }
  309. AVInputFormat ff_rpl_demuxer = {
  310. .name = "rpl",
  311. .long_name = NULL_IF_CONFIG_SMALL("RPL / ARMovie"),
  312. .priv_data_size = sizeof(RPLContext),
  313. .read_probe = rpl_probe,
  314. .read_header = rpl_read_header,
  315. .read_packet = rpl_read_packet,
  316. };