PageRenderTime 22ms CodeModel.GetById 32ms RepoModel.GetById 0ms app.codeStats 0ms

/lib/ffmpeg/libavformat/rpl.c

https://github.com/energy6/xbmc
C | 359 lines | 260 code | 42 blank | 57 comment | 67 complexity | d40b2afe187b5333ae6458cf40daf947 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 "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 url_feof(pb) ? -1 : 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, AVFormatParameters *ap)
  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. case 130:
  150. vst->codec->codec_id = CODEC_ID_ESCAPE130;
  151. break;
  152. default:
  153. av_log(s, AV_LOG_WARNING,
  154. "RPL video format %i not supported yet!\n",
  155. vst->codec->codec_tag);
  156. vst->codec->codec_id = 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 = 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 = 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 = CODEC_ID_PCM_U8;
  194. break;
  195. } else if (ast->codec->bits_per_coded_sample == 4) {
  196. ast->codec->codec_id = CODEC_ID_ADPCM_IMA_EA_SEAD;
  197. break;
  198. }
  199. break;
  200. }
  201. if (ast->codec->codec_id == CODEC_ID_NONE) {
  202. av_log(s, AV_LOG_WARNING,
  203. "RPL audio format %i not supported yet!\n",
  204. audio_format);
  205. }
  206. avpriv_set_pts_info(ast, 32, 1, ast->codec->bit_rate);
  207. } else {
  208. for (i = 0; i < 3; i++)
  209. error |= read_line(pb, line, sizeof(line));
  210. }
  211. rpl->frames_per_chunk = read_line_and_int(pb, &error); // video frames per chunk
  212. if (rpl->frames_per_chunk > 1 && vst->codec->codec_tag != 124)
  213. av_log(s, AV_LOG_WARNING,
  214. "Don't know how to split frames for video format %i. "
  215. "Video stream will be broken!\n", vst->codec->codec_tag);
  216. number_of_chunks = read_line_and_int(pb, &error); // number of chunks in the file
  217. // The number in the header is actually the index of the last chunk.
  218. number_of_chunks++;
  219. error |= read_line(pb, line, sizeof(line)); // "even" chunk size in bytes
  220. error |= read_line(pb, line, sizeof(line)); // "odd" chunk size in bytes
  221. chunk_catalog_offset = // offset of the "chunk catalog"
  222. read_line_and_int(pb, &error); // (file index)
  223. error |= read_line(pb, line, sizeof(line)); // offset to "helpful" sprite
  224. error |= read_line(pb, line, sizeof(line)); // size of "helpful" sprite
  225. error |= read_line(pb, line, sizeof(line)); // offset to key frame list
  226. // Read the index
  227. avio_seek(pb, chunk_catalog_offset, SEEK_SET);
  228. total_audio_size = 0;
  229. for (i = 0; !error && i < number_of_chunks; i++) {
  230. int64_t offset, video_size, audio_size;
  231. error |= read_line(pb, line, sizeof(line));
  232. if (3 != sscanf(line, "%"PRId64" , %"PRId64" ; %"PRId64,
  233. &offset, &video_size, &audio_size))
  234. error = -1;
  235. av_add_index_entry(vst, offset, i * rpl->frames_per_chunk,
  236. video_size, rpl->frames_per_chunk, 0);
  237. if (ast)
  238. av_add_index_entry(ast, offset + video_size, total_audio_size,
  239. audio_size, audio_size * 8, 0);
  240. total_audio_size += audio_size * 8;
  241. }
  242. if (error) return AVERROR(EIO);
  243. return 0;
  244. }
  245. static int rpl_read_packet(AVFormatContext *s, AVPacket *pkt)
  246. {
  247. RPLContext *rpl = s->priv_data;
  248. AVIOContext *pb = s->pb;
  249. AVStream* stream;
  250. AVIndexEntry* index_entry;
  251. uint32_t ret;
  252. if (rpl->chunk_part == s->nb_streams) {
  253. rpl->chunk_number++;
  254. rpl->chunk_part = 0;
  255. }
  256. stream = s->streams[rpl->chunk_part];
  257. if (rpl->chunk_number >= stream->nb_index_entries)
  258. return -1;
  259. index_entry = &stream->index_entries[rpl->chunk_number];
  260. if (rpl->frame_in_part == 0)
  261. if (avio_seek(pb, index_entry->pos, SEEK_SET) < 0)
  262. return AVERROR(EIO);
  263. if (stream->codec->codec_type == AVMEDIA_TYPE_VIDEO &&
  264. stream->codec->codec_tag == 124) {
  265. // We have to split Escape 124 frames because there are
  266. // multiple frames per chunk in Escape 124 samples.
  267. uint32_t frame_size;
  268. avio_skip(pb, 4); /* flags */
  269. frame_size = avio_rl32(pb);
  270. if (avio_seek(pb, -8, SEEK_CUR) < 0)
  271. return AVERROR(EIO);
  272. ret = av_get_packet(pb, pkt, frame_size);
  273. if (ret != frame_size) {
  274. av_free_packet(pkt);
  275. return AVERROR(EIO);
  276. }
  277. pkt->duration = 1;
  278. pkt->pts = index_entry->timestamp + rpl->frame_in_part;
  279. pkt->stream_index = rpl->chunk_part;
  280. rpl->frame_in_part++;
  281. if (rpl->frame_in_part == rpl->frames_per_chunk) {
  282. rpl->frame_in_part = 0;
  283. rpl->chunk_part++;
  284. }
  285. } else {
  286. ret = av_get_packet(pb, pkt, index_entry->size);
  287. if (ret != index_entry->size) {
  288. av_free_packet(pkt);
  289. return AVERROR(EIO);
  290. }
  291. if (stream->codec->codec_type == AVMEDIA_TYPE_VIDEO) {
  292. // frames_per_chunk should always be one here; the header
  293. // parsing will warn if it isn't.
  294. pkt->duration = rpl->frames_per_chunk;
  295. } else {
  296. // All the audio codecs supported in this container
  297. // (at least so far) are constant-bitrate.
  298. pkt->duration = ret * 8;
  299. }
  300. pkt->pts = index_entry->timestamp;
  301. pkt->stream_index = rpl->chunk_part;
  302. rpl->chunk_part++;
  303. }
  304. // None of the Escape formats have keyframes, and the ADPCM
  305. // format used doesn't have keyframes.
  306. if (rpl->chunk_number == 0 && rpl->frame_in_part == 0)
  307. pkt->flags |= AV_PKT_FLAG_KEY;
  308. return ret;
  309. }
  310. AVInputFormat ff_rpl_demuxer = {
  311. .name = "rpl",
  312. .long_name = NULL_IF_CONFIG_SMALL("RPL/ARMovie format"),
  313. .priv_data_size = sizeof(RPLContext),
  314. .read_probe = rpl_probe,
  315. .read_header = rpl_read_header,
  316. .read_packet = rpl_read_packet,
  317. };