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.

380 lines
13KB

  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 <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(const 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 avio_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. const char *endptr;
  110. char audio_type[RPL_LINE_LENGTH];
  111. uint32_t i;
  112. int32_t audio_format, chunk_catalog_offset, number_of_chunks;
  113. AVRational fps;
  114. char line[RPL_LINE_LENGTH];
  115. // The header for RPL/ARMovie files is 21 lines of text
  116. // containing the various header fields. The fields are always
  117. // in the same order, and other text besides the first
  118. // number usually isn't important.
  119. // (The spec says that there exists some significance
  120. // for the text in a few cases; samples needed.)
  121. error |= read_line(pb, line, sizeof(line)); // ARMovie
  122. error |= read_line(pb, line, sizeof(line)); // movie name
  123. av_dict_set(&s->metadata, "title" , line, 0);
  124. error |= read_line(pb, line, sizeof(line)); // date/copyright
  125. av_dict_set(&s->metadata, "copyright", line, 0);
  126. error |= read_line(pb, line, sizeof(line)); // author and other
  127. av_dict_set(&s->metadata, "author" , line, 0);
  128. // video headers
  129. vst = avformat_new_stream(s, NULL);
  130. if (!vst)
  131. return AVERROR(ENOMEM);
  132. vst->codecpar->codec_type = AVMEDIA_TYPE_VIDEO;
  133. vst->codecpar->codec_tag = read_line_and_int(pb, &error); // video format
  134. vst->codecpar->width = read_line_and_int(pb, &error); // video width
  135. vst->codecpar->height = read_line_and_int(pb, &error); // video height
  136. vst->codecpar->bits_per_coded_sample = read_line_and_int(pb, &error); // video bits per sample
  137. error |= read_line(pb, line, sizeof(line)); // video frames per second
  138. fps = read_fps(line, &error);
  139. avpriv_set_pts_info(vst, 32, fps.den, fps.num);
  140. // Figure out the video codec
  141. switch (vst->codecpar->codec_tag) {
  142. #if 0
  143. case 122:
  144. vst->codecpar->codec_id = AV_CODEC_ID_ESCAPE122;
  145. break;
  146. #endif
  147. case 124:
  148. vst->codecpar->codec_id = AV_CODEC_ID_ESCAPE124;
  149. // The header is wrong here, at least sometimes
  150. vst->codecpar->bits_per_coded_sample = 16;
  151. break;
  152. case 130:
  153. vst->codecpar->codec_id = AV_CODEC_ID_ESCAPE130;
  154. break;
  155. default:
  156. avpriv_report_missing_feature(s, "Video format %s",
  157. av_fourcc2str(vst->codecpar->codec_tag));
  158. vst->codecpar->codec_id = AV_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->codecpar->codec_type = AVMEDIA_TYPE_AUDIO;
  169. ast->codecpar->codec_tag = audio_format;
  170. ast->codecpar->sample_rate = read_line_and_int(pb, &error); // audio bitrate
  171. ast->codecpar->channels = read_line_and_int(pb, &error); // number of audio channels
  172. error |= read_line(pb, line, sizeof(line));
  173. ast->codecpar->bits_per_coded_sample = read_int(line, &endptr, &error); // audio bits per sample
  174. strcpy(audio_type, endptr);
  175. // At least one sample uses 0 for ADPCM, which is really 4 bits
  176. // per sample.
  177. if (ast->codecpar->bits_per_coded_sample == 0)
  178. ast->codecpar->bits_per_coded_sample = 4;
  179. ast->codecpar->bit_rate = ast->codecpar->sample_rate *
  180. ast->codecpar->bits_per_coded_sample *
  181. ast->codecpar->channels;
  182. ast->codecpar->codec_id = AV_CODEC_ID_NONE;
  183. switch (audio_format) {
  184. case 1:
  185. if (ast->codecpar->bits_per_coded_sample == 16) {
  186. // 16-bit audio is always signed
  187. ast->codecpar->codec_id = AV_CODEC_ID_PCM_S16LE;
  188. break;
  189. } else if (ast->codecpar->bits_per_coded_sample == 8) {
  190. if(av_stristr(audio_type, "unsigned") != NULL) {
  191. ast->codecpar->codec_id = AV_CODEC_ID_PCM_U8;
  192. break;
  193. } else if(av_stristr(audio_type, "linear") != NULL) {
  194. ast->codecpar->codec_id = AV_CODEC_ID_PCM_S8;
  195. break;
  196. } else {
  197. ast->codecpar->codec_id = AV_CODEC_ID_PCM_VIDC;
  198. break;
  199. }
  200. }
  201. // There are some other formats listed as legal per the spec;
  202. // samples needed.
  203. break;
  204. case 101:
  205. if (ast->codecpar->bits_per_coded_sample == 8) {
  206. // The samples with this kind of audio that I have
  207. // are all unsigned.
  208. ast->codecpar->codec_id = AV_CODEC_ID_PCM_U8;
  209. break;
  210. } else if (ast->codecpar->bits_per_coded_sample == 4) {
  211. ast->codecpar->codec_id = AV_CODEC_ID_ADPCM_IMA_EA_SEAD;
  212. break;
  213. }
  214. break;
  215. }
  216. if (ast->codecpar->codec_id == AV_CODEC_ID_NONE)
  217. avpriv_request_sample(s, "Audio format %"PRId32,
  218. audio_format);
  219. avpriv_set_pts_info(ast, 32, 1, ast->codecpar->bit_rate);
  220. } else {
  221. for (i = 0; i < 3; i++)
  222. error |= read_line(pb, line, sizeof(line));
  223. }
  224. rpl->frames_per_chunk = read_line_and_int(pb, &error); // video frames per chunk
  225. if (rpl->frames_per_chunk > 1 && vst->codecpar->codec_tag != 124)
  226. av_log(s, AV_LOG_WARNING,
  227. "Don't know how to split frames for video format %s. "
  228. "Video stream will be broken!\n", av_fourcc2str(vst->codecpar->codec_tag));
  229. number_of_chunks = read_line_and_int(pb, &error); // number of chunks in the file
  230. // The number in the header is actually the index of the last chunk.
  231. number_of_chunks++;
  232. error |= read_line(pb, line, sizeof(line)); // "even" chunk size in bytes
  233. error |= read_line(pb, line, sizeof(line)); // "odd" chunk size in bytes
  234. chunk_catalog_offset = // offset of the "chunk catalog"
  235. read_line_and_int(pb, &error); // (file index)
  236. error |= read_line(pb, line, sizeof(line)); // offset to "helpful" sprite
  237. error |= read_line(pb, line, sizeof(line)); // size of "helpful" sprite
  238. error |= read_line(pb, line, sizeof(line)); // offset to key frame list
  239. // Read the index
  240. avio_seek(pb, chunk_catalog_offset, SEEK_SET);
  241. total_audio_size = 0;
  242. for (i = 0; !error && i < number_of_chunks; i++) {
  243. int64_t offset, video_size, audio_size;
  244. error |= read_line(pb, line, sizeof(line));
  245. if (3 != sscanf(line, "%"SCNd64" , %"SCNd64" ; %"SCNd64,
  246. &offset, &video_size, &audio_size)) {
  247. error = -1;
  248. continue;
  249. }
  250. av_add_index_entry(vst, offset, i * rpl->frames_per_chunk,
  251. video_size, rpl->frames_per_chunk, 0);
  252. if (ast)
  253. av_add_index_entry(ast, offset + video_size, total_audio_size,
  254. audio_size, audio_size * 8, 0);
  255. total_audio_size += audio_size * 8;
  256. }
  257. if (error) return AVERROR(EIO);
  258. return 0;
  259. }
  260. static int rpl_read_packet(AVFormatContext *s, AVPacket *pkt)
  261. {
  262. RPLContext *rpl = s->priv_data;
  263. AVIOContext *pb = s->pb;
  264. AVStream* stream;
  265. AVIndexEntry* index_entry;
  266. int ret;
  267. if (rpl->chunk_part == s->nb_streams) {
  268. rpl->chunk_number++;
  269. rpl->chunk_part = 0;
  270. }
  271. stream = s->streams[rpl->chunk_part];
  272. if (rpl->chunk_number >= stream->nb_index_entries)
  273. return AVERROR_EOF;
  274. index_entry = &stream->index_entries[rpl->chunk_number];
  275. if (rpl->frame_in_part == 0)
  276. if (avio_seek(pb, index_entry->pos, SEEK_SET) < 0)
  277. return AVERROR(EIO);
  278. if (stream->codecpar->codec_type == AVMEDIA_TYPE_VIDEO &&
  279. stream->codecpar->codec_tag == 124) {
  280. // We have to split Escape 124 frames because there are
  281. // multiple frames per chunk in Escape 124 samples.
  282. uint32_t frame_size;
  283. avio_skip(pb, 4); /* flags */
  284. frame_size = avio_rl32(pb);
  285. if (avio_seek(pb, -8, SEEK_CUR) < 0)
  286. return AVERROR(EIO);
  287. ret = av_get_packet(pb, pkt, frame_size);
  288. if (ret < 0)
  289. return ret;
  290. if (ret != frame_size) {
  291. av_packet_unref(pkt);
  292. return AVERROR(EIO);
  293. }
  294. pkt->duration = 1;
  295. pkt->pts = index_entry->timestamp + rpl->frame_in_part;
  296. pkt->stream_index = rpl->chunk_part;
  297. rpl->frame_in_part++;
  298. if (rpl->frame_in_part == rpl->frames_per_chunk) {
  299. rpl->frame_in_part = 0;
  300. rpl->chunk_part++;
  301. }
  302. } else {
  303. ret = av_get_packet(pb, pkt, index_entry->size);
  304. if (ret < 0)
  305. return ret;
  306. if (ret != index_entry->size) {
  307. av_packet_unref(pkt);
  308. return AVERROR(EIO);
  309. }
  310. if (stream->codecpar->codec_type == AVMEDIA_TYPE_VIDEO) {
  311. // frames_per_chunk should always be one here; the header
  312. // parsing will warn if it isn't.
  313. pkt->duration = rpl->frames_per_chunk;
  314. } else {
  315. // All the audio codecs supported in this container
  316. // (at least so far) are constant-bitrate.
  317. pkt->duration = ret * 8;
  318. }
  319. pkt->pts = index_entry->timestamp;
  320. pkt->stream_index = rpl->chunk_part;
  321. rpl->chunk_part++;
  322. }
  323. // None of the Escape formats have keyframes, and the ADPCM
  324. // format used doesn't have keyframes.
  325. if (rpl->chunk_number == 0 && rpl->frame_in_part == 0)
  326. pkt->flags |= AV_PKT_FLAG_KEY;
  327. return ret;
  328. }
  329. AVInputFormat ff_rpl_demuxer = {
  330. .name = "rpl",
  331. .long_name = NULL_IF_CONFIG_SMALL("RPL / ARMovie"),
  332. .priv_data_size = sizeof(RPLContext),
  333. .read_probe = rpl_probe,
  334. .read_header = rpl_read_header,
  335. .read_packet = rpl_read_packet,
  336. };