r3d.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394
  1. /*
  2. * R3D REDCODE demuxer
  3. * Copyright (c) 2008 Baptiste Coudurier <baptiste dot coudurier at gmail dot com>
  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. //#define DEBUG
  22. #include "libavutil/intreadwrite.h"
  23. #include "libavutil/dict.h"
  24. #include "avformat.h"
  25. typedef struct {
  26. unsigned video_offsets_count;
  27. unsigned *video_offsets;
  28. unsigned rdvo_offset;
  29. } R3DContext;
  30. typedef struct {
  31. unsigned size;
  32. uint32_t tag;
  33. uint64_t offset;
  34. } Atom;
  35. static int read_atom(AVFormatContext *s, Atom *atom)
  36. {
  37. atom->offset = avio_tell(s->pb);
  38. atom->size = avio_rb32(s->pb);
  39. if (atom->size < 8)
  40. return -1;
  41. atom->tag = avio_rl32(s->pb);
  42. av_dlog(s, "atom %u %.4s offset %#"PRIx64"\n",
  43. atom->size, (char*)&atom->tag, atom->offset);
  44. return atom->size;
  45. }
  46. static int r3d_read_red1(AVFormatContext *s)
  47. {
  48. AVStream *st = av_new_stream(s, 0);
  49. char filename[258];
  50. int tmp;
  51. int av_unused tmp2;
  52. if (!st)
  53. return AVERROR(ENOMEM);
  54. st->codec->codec_type = AVMEDIA_TYPE_VIDEO;
  55. st->codec->codec_id = CODEC_ID_JPEG2000;
  56. tmp = avio_r8(s->pb); // major version
  57. tmp2 = avio_r8(s->pb); // minor version
  58. av_dlog(s, "version %d.%d\n", tmp, tmp2);
  59. tmp = avio_rb16(s->pb); // unknown
  60. av_dlog(s, "unknown1 %d\n", tmp);
  61. tmp = avio_rb32(s->pb);
  62. av_set_pts_info(st, 32, 1, tmp);
  63. tmp = avio_rb32(s->pb); // filenum
  64. av_dlog(s, "filenum %d\n", tmp);
  65. avio_skip(s->pb, 32); // unknown
  66. st->codec->width = avio_rb32(s->pb);
  67. st->codec->height = avio_rb32(s->pb);
  68. tmp = avio_rb16(s->pb); // unknown
  69. av_dlog(s, "unknown2 %d\n", tmp);
  70. st->codec->time_base.den = avio_rb16(s->pb);
  71. st->codec->time_base.num = avio_rb16(s->pb);
  72. tmp = avio_r8(s->pb); // audio channels
  73. av_dlog(s, "audio channels %d\n", tmp);
  74. if (tmp > 0) {
  75. AVStream *ast = av_new_stream(s, 1);
  76. if (!ast)
  77. return AVERROR(ENOMEM);
  78. ast->codec->codec_type = AVMEDIA_TYPE_AUDIO;
  79. ast->codec->codec_id = CODEC_ID_PCM_S32BE;
  80. ast->codec->channels = tmp;
  81. av_set_pts_info(ast, 32, 1, st->time_base.den);
  82. }
  83. avio_read(s->pb, filename, 257);
  84. filename[sizeof(filename)-1] = 0;
  85. av_dict_set(&st->metadata, "filename", filename, 0);
  86. av_dlog(s, "filename %s\n", filename);
  87. av_dlog(s, "resolution %dx%d\n", st->codec->width, st->codec->height);
  88. av_dlog(s, "timescale %d\n", st->time_base.den);
  89. av_dlog(s, "frame rate %d/%d\n",
  90. st->codec->time_base.num, st->codec->time_base.den);
  91. return 0;
  92. }
  93. static int r3d_read_rdvo(AVFormatContext *s, Atom *atom)
  94. {
  95. R3DContext *r3d = s->priv_data;
  96. AVStream *st = s->streams[0];
  97. int i;
  98. r3d->video_offsets_count = (atom->size - 8) / 4;
  99. r3d->video_offsets = av_malloc(atom->size);
  100. if (!r3d->video_offsets)
  101. return AVERROR(ENOMEM);
  102. for (i = 0; i < r3d->video_offsets_count; i++) {
  103. r3d->video_offsets[i] = avio_rb32(s->pb);
  104. if (!r3d->video_offsets[i]) {
  105. r3d->video_offsets_count = i;
  106. break;
  107. }
  108. av_dlog(s, "video offset %d: %#x\n", i, r3d->video_offsets[i]);
  109. }
  110. if (st->codec->time_base.den)
  111. st->duration = (uint64_t)r3d->video_offsets_count*
  112. st->time_base.den*st->codec->time_base.num/st->codec->time_base.den;
  113. av_dlog(s, "duration %"PRId64"\n", st->duration);
  114. return 0;
  115. }
  116. static void r3d_read_reos(AVFormatContext *s)
  117. {
  118. R3DContext *r3d = s->priv_data;
  119. int av_unused tmp;
  120. r3d->rdvo_offset = avio_rb32(s->pb);
  121. avio_rb32(s->pb); // rdvs offset
  122. avio_rb32(s->pb); // rdao offset
  123. avio_rb32(s->pb); // rdas offset
  124. tmp = avio_rb32(s->pb);
  125. av_dlog(s, "num video chunks %d\n", tmp);
  126. tmp = avio_rb32(s->pb);
  127. av_dlog(s, "num audio chunks %d\n", tmp);
  128. avio_skip(s->pb, 6*4);
  129. }
  130. static int r3d_read_header(AVFormatContext *s, AVFormatParameters *ap)
  131. {
  132. R3DContext *r3d = s->priv_data;
  133. Atom atom;
  134. int ret;
  135. if (read_atom(s, &atom) < 0) {
  136. av_log(s, AV_LOG_ERROR, "error reading atom\n");
  137. return -1;
  138. }
  139. if (atom.tag == MKTAG('R','E','D','1')) {
  140. if ((ret = r3d_read_red1(s)) < 0) {
  141. av_log(s, AV_LOG_ERROR, "error parsing 'red1' atom\n");
  142. return ret;
  143. }
  144. } else {
  145. av_log(s, AV_LOG_ERROR, "could not find 'red1' atom\n");
  146. return -1;
  147. }
  148. s->data_offset = avio_tell(s->pb);
  149. av_dlog(s, "data offset %#"PRIx64"\n", s->data_offset);
  150. if (!s->pb->seekable)
  151. return 0;
  152. // find REOB/REOF/REOS to load index
  153. avio_seek(s->pb, avio_size(s->pb)-48-8, SEEK_SET);
  154. if (read_atom(s, &atom) < 0)
  155. av_log(s, AV_LOG_ERROR, "error reading end atom\n");
  156. if (atom.tag != MKTAG('R','E','O','B') &&
  157. atom.tag != MKTAG('R','E','O','F') &&
  158. atom.tag != MKTAG('R','E','O','S'))
  159. goto out;
  160. r3d_read_reos(s);
  161. if (r3d->rdvo_offset) {
  162. avio_seek(s->pb, r3d->rdvo_offset, SEEK_SET);
  163. if (read_atom(s, &atom) < 0)
  164. av_log(s, AV_LOG_ERROR, "error reading 'rdvo' atom\n");
  165. if (atom.tag == MKTAG('R','D','V','O')) {
  166. if (r3d_read_rdvo(s, &atom) < 0)
  167. av_log(s, AV_LOG_ERROR, "error parsing 'rdvo' atom\n");
  168. }
  169. }
  170. out:
  171. avio_seek(s->pb, s->data_offset, SEEK_SET);
  172. return 0;
  173. }
  174. static int r3d_read_redv(AVFormatContext *s, AVPacket *pkt, Atom *atom)
  175. {
  176. AVStream *st = s->streams[0];
  177. int tmp;
  178. int av_unused tmp2;
  179. uint64_t pos = avio_tell(s->pb);
  180. unsigned dts;
  181. int ret;
  182. dts = avio_rb32(s->pb);
  183. tmp = avio_rb32(s->pb);
  184. av_dlog(s, "frame num %d\n", tmp);
  185. tmp = avio_r8(s->pb); // major version
  186. tmp2 = avio_r8(s->pb); // minor version
  187. av_dlog(s, "version %d.%d\n", tmp, tmp2);
  188. tmp = avio_rb16(s->pb); // unknown
  189. av_dlog(s, "unknown %d\n", tmp);
  190. if (tmp > 4) {
  191. tmp = avio_rb16(s->pb); // unknown
  192. av_dlog(s, "unknown %d\n", tmp);
  193. tmp = avio_rb16(s->pb); // unknown
  194. av_dlog(s, "unknown %d\n", tmp);
  195. tmp = avio_rb32(s->pb);
  196. av_dlog(s, "width %d\n", tmp);
  197. tmp = avio_rb32(s->pb);
  198. av_dlog(s, "height %d\n", tmp);
  199. tmp = avio_rb32(s->pb);
  200. av_dlog(s, "metadata len %d\n", tmp);
  201. }
  202. tmp = atom->size - 8 - (avio_tell(s->pb) - pos);
  203. if (tmp < 0)
  204. return -1;
  205. ret = av_get_packet(s->pb, pkt, tmp);
  206. if (ret < 0) {
  207. av_log(s, AV_LOG_ERROR, "error reading video packet\n");
  208. return -1;
  209. }
  210. pkt->stream_index = 0;
  211. pkt->dts = dts;
  212. if (st->codec->time_base.den)
  213. pkt->duration = (uint64_t)st->time_base.den*
  214. st->codec->time_base.num/st->codec->time_base.den;
  215. av_dlog(s, "pkt dts %"PRId64" duration %d\n", pkt->dts, pkt->duration);
  216. return 0;
  217. }
  218. static int r3d_read_reda(AVFormatContext *s, AVPacket *pkt, Atom *atom)
  219. {
  220. AVStream *st = s->streams[1];
  221. int av_unused tmp, tmp2;
  222. int samples, size;
  223. uint64_t pos = avio_tell(s->pb);
  224. unsigned dts;
  225. int ret;
  226. dts = avio_rb32(s->pb);
  227. st->codec->sample_rate = avio_rb32(s->pb);
  228. samples = avio_rb32(s->pb);
  229. tmp = avio_rb32(s->pb);
  230. av_dlog(s, "packet num %d\n", tmp);
  231. tmp = avio_rb16(s->pb); // unkown
  232. av_dlog(s, "unknown %d\n", tmp);
  233. tmp = avio_r8(s->pb); // major version
  234. tmp2 = avio_r8(s->pb); // minor version
  235. av_dlog(s, "version %d.%d\n", tmp, tmp2);
  236. tmp = avio_rb32(s->pb); // unknown
  237. av_dlog(s, "unknown %d\n", tmp);
  238. size = atom->size - 8 - (avio_tell(s->pb) - pos);
  239. if (size < 0)
  240. return -1;
  241. ret = av_get_packet(s->pb, pkt, size);
  242. if (ret < 0) {
  243. av_log(s, AV_LOG_ERROR, "error reading audio packet\n");
  244. return ret;
  245. }
  246. pkt->stream_index = 1;
  247. pkt->dts = dts;
  248. pkt->duration = av_rescale(samples, st->time_base.den, st->codec->sample_rate);
  249. av_dlog(s, "pkt dts %"PRId64" duration %d samples %d sample rate %d\n",
  250. pkt->dts, pkt->duration, samples, st->codec->sample_rate);
  251. return 0;
  252. }
  253. static int r3d_read_packet(AVFormatContext *s, AVPacket *pkt)
  254. {
  255. Atom atom;
  256. int err = 0;
  257. while (!err) {
  258. if (read_atom(s, &atom) < 0) {
  259. err = -1;
  260. break;
  261. }
  262. switch (atom.tag) {
  263. case MKTAG('R','E','D','V'):
  264. if (s->streams[0]->discard == AVDISCARD_ALL)
  265. goto skip;
  266. if (!(err = r3d_read_redv(s, pkt, &atom)))
  267. return 0;
  268. break;
  269. case MKTAG('R','E','D','A'):
  270. if (s->nb_streams < 2)
  271. return -1;
  272. if (s->streams[1]->discard == AVDISCARD_ALL)
  273. goto skip;
  274. if (!(err = r3d_read_reda(s, pkt, &atom)))
  275. return 0;
  276. break;
  277. default:
  278. skip:
  279. avio_skip(s->pb, atom.size-8);
  280. }
  281. }
  282. return err;
  283. }
  284. static int r3d_probe(AVProbeData *p)
  285. {
  286. if (AV_RL32(p->buf + 4) == MKTAG('R','E','D','1'))
  287. return AVPROBE_SCORE_MAX;
  288. return 0;
  289. }
  290. static int r3d_seek(AVFormatContext *s, int stream_index, int64_t sample_time, int flags)
  291. {
  292. AVStream *st = s->streams[0]; // video stream
  293. R3DContext *r3d = s->priv_data;
  294. int frame_num;
  295. if (!st->codec->time_base.num || !st->time_base.den)
  296. return -1;
  297. frame_num = sample_time*st->codec->time_base.den/
  298. ((int64_t)st->codec->time_base.num*st->time_base.den);
  299. av_dlog(s, "seek frame num %d timestamp %"PRId64"\n",
  300. frame_num, sample_time);
  301. if (frame_num < r3d->video_offsets_count) {
  302. avio_seek(s->pb, r3d->video_offsets_count, SEEK_SET);
  303. } else {
  304. av_log(s, AV_LOG_ERROR, "could not seek to frame %d\n", frame_num);
  305. return -1;
  306. }
  307. return 0;
  308. }
  309. static int r3d_close(AVFormatContext *s)
  310. {
  311. R3DContext *r3d = s->priv_data;
  312. av_freep(&r3d->video_offsets);
  313. return 0;
  314. }
  315. AVInputFormat ff_r3d_demuxer = {
  316. "r3d",
  317. NULL_IF_CONFIG_SMALL("REDCODE R3D format"),
  318. sizeof(R3DContext),
  319. r3d_probe,
  320. r3d_read_header,
  321. r3d_read_packet,
  322. r3d_close,
  323. r3d_seek,
  324. };