/libavformat/rpl.c

http://github.com/FFmpeg/FFmpeg · C · 389 lines · 288 code · 44 blank · 57 comment · 85 complexity · e1ebb0d9c14564e8e77fb77fe1f876a7 MD5 · raw file

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