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.

407 lines
13KB

  1. /*
  2. * NuppelVideo demuxer.
  3. * Copyright (c) 2006 Reimar Doeffinger
  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/channel_layout.h"
  22. #include "libavutil/imgutils.h"
  23. #include "libavutil/intreadwrite.h"
  24. #include "libavutil/intfloat.h"
  25. #include "avformat.h"
  26. #include "internal.h"
  27. #include "riff.h"
  28. static const AVCodecTag nuv_audio_tags[] = {
  29. { AV_CODEC_ID_PCM_S16LE, MKTAG('R', 'A', 'W', 'A') },
  30. { AV_CODEC_ID_MP3, MKTAG('L', 'A', 'M', 'E') },
  31. { AV_CODEC_ID_NONE, 0 },
  32. };
  33. typedef struct NUVContext {
  34. int v_id;
  35. int a_id;
  36. int rtjpg_video;
  37. } NUVContext;
  38. typedef enum {
  39. NUV_VIDEO = 'V',
  40. NUV_EXTRADATA = 'D',
  41. NUV_AUDIO = 'A',
  42. NUV_SEEKP = 'R',
  43. NUV_MYTHEXT = 'X'
  44. } nuv_frametype;
  45. static int nuv_probe(const AVProbeData *p)
  46. {
  47. if (!memcmp(p->buf, "NuppelVideo", 12))
  48. return AVPROBE_SCORE_MAX;
  49. if (!memcmp(p->buf, "MythTVVideo", 12))
  50. return AVPROBE_SCORE_MAX;
  51. return 0;
  52. }
  53. /// little macro to sanitize packet size
  54. #define PKTSIZE(s) (s & 0xffffff)
  55. /**
  56. * @brief read until we found all data needed for decoding
  57. * @param vst video stream of which to change parameters
  58. * @param ast video stream of which to change parameters
  59. * @param myth set if this is a MythTVVideo format file
  60. * @return 0 or AVERROR code
  61. */
  62. static int get_codec_data(AVFormatContext *s, AVIOContext *pb, AVStream *vst,
  63. AVStream *ast, int myth)
  64. {
  65. nuv_frametype frametype;
  66. if (!vst && !myth)
  67. return 1; // no codec data needed
  68. while (!avio_feof(pb)) {
  69. int size, subtype, ret;
  70. frametype = avio_r8(pb);
  71. switch (frametype) {
  72. case NUV_EXTRADATA:
  73. subtype = avio_r8(pb);
  74. avio_skip(pb, 6);
  75. size = PKTSIZE(avio_rl32(pb));
  76. if (vst && subtype == 'R') {
  77. if ((ret = ff_get_extradata(NULL, vst->codecpar, pb, size)) < 0)
  78. return ret;
  79. size = 0;
  80. if (!myth)
  81. return 0;
  82. }
  83. break;
  84. case NUV_MYTHEXT:
  85. avio_skip(pb, 7);
  86. size = PKTSIZE(avio_rl32(pb));
  87. if (size != 128 * 4)
  88. break;
  89. avio_rl32(pb); // version
  90. if (vst) {
  91. vst->codecpar->codec_tag = avio_rl32(pb);
  92. vst->codecpar->codec_id =
  93. ff_codec_get_id(ff_codec_bmp_tags, vst->codecpar->codec_tag);
  94. if (vst->codecpar->codec_tag == MKTAG('R', 'J', 'P', 'G'))
  95. vst->codecpar->codec_id = AV_CODEC_ID_NUV;
  96. } else
  97. avio_skip(pb, 4);
  98. if (ast) {
  99. int id;
  100. ast->codecpar->codec_tag = avio_rl32(pb);
  101. ast->codecpar->sample_rate = avio_rl32(pb);
  102. if (ast->codecpar->sample_rate <= 0) {
  103. av_log(s, AV_LOG_ERROR, "Invalid sample rate %d\n", ast->codecpar->sample_rate);
  104. return AVERROR_INVALIDDATA;
  105. }
  106. ast->codecpar->bits_per_coded_sample = avio_rl32(pb);
  107. ast->codecpar->channels = avio_rl32(pb);
  108. ast->codecpar->channel_layout = 0;
  109. if (ast->codecpar->channels <= 0) {
  110. av_log(s, AV_LOG_ERROR, "Invalid channels %d\n", ast->codecpar->channels);
  111. return AVERROR_INVALIDDATA;
  112. }
  113. id = ff_wav_codec_get_id(ast->codecpar->codec_tag,
  114. ast->codecpar->bits_per_coded_sample);
  115. if (id == AV_CODEC_ID_NONE) {
  116. id = ff_codec_get_id(nuv_audio_tags, ast->codecpar->codec_tag);
  117. if (id == AV_CODEC_ID_PCM_S16LE)
  118. id = ff_get_pcm_codec_id(ast->codecpar->bits_per_coded_sample,
  119. 0, 0, ~1);
  120. }
  121. ast->codecpar->codec_id = id;
  122. ast->need_parsing = AVSTREAM_PARSE_FULL;
  123. } else
  124. avio_skip(pb, 4 * 4);
  125. size -= 6 * 4;
  126. avio_skip(pb, size);
  127. return 0;
  128. case NUV_SEEKP:
  129. size = 11;
  130. break;
  131. default:
  132. avio_skip(pb, 7);
  133. size = PKTSIZE(avio_rl32(pb));
  134. break;
  135. }
  136. avio_skip(pb, size);
  137. }
  138. return 0;
  139. }
  140. static int nuv_header(AVFormatContext *s)
  141. {
  142. NUVContext *ctx = s->priv_data;
  143. AVIOContext *pb = s->pb;
  144. char id_string[12];
  145. double aspect, fps;
  146. int is_mythtv, width, height, v_packs, a_packs, ret;
  147. AVStream *vst = NULL, *ast = NULL;
  148. avio_read(pb, id_string, 12);
  149. is_mythtv = !memcmp(id_string, "MythTVVideo", 12);
  150. avio_skip(pb, 5); // version string
  151. avio_skip(pb, 3); // padding
  152. width = avio_rl32(pb);
  153. height = avio_rl32(pb);
  154. avio_rl32(pb); // unused, "desiredwidth"
  155. avio_rl32(pb); // unused, "desiredheight"
  156. avio_r8(pb); // 'P' == progressive, 'I' == interlaced
  157. avio_skip(pb, 3); // padding
  158. aspect = av_int2double(avio_rl64(pb));
  159. if (aspect > 0.9999 && aspect < 1.0001)
  160. aspect = 4.0 / 3.0;
  161. fps = av_int2double(avio_rl64(pb));
  162. if (fps < 0.0f) {
  163. if (s->error_recognition & AV_EF_EXPLODE) {
  164. av_log(s, AV_LOG_ERROR, "Invalid frame rate %f\n", fps);
  165. return AVERROR_INVALIDDATA;
  166. } else {
  167. av_log(s, AV_LOG_WARNING, "Invalid frame rate %f, setting to 0.\n", fps);
  168. fps = 0.0f;
  169. }
  170. }
  171. // number of packets per stream type, -1 means unknown, e.g. streaming
  172. v_packs = avio_rl32(pb);
  173. a_packs = avio_rl32(pb);
  174. avio_rl32(pb); // text
  175. avio_rl32(pb); // keyframe distance (?)
  176. if (v_packs) {
  177. vst = avformat_new_stream(s, NULL);
  178. if (!vst)
  179. return AVERROR(ENOMEM);
  180. ctx->v_id = vst->index;
  181. ret = av_image_check_size(width, height, 0, s);
  182. if (ret < 0)
  183. return ret;
  184. vst->codecpar->codec_type = AVMEDIA_TYPE_VIDEO;
  185. vst->codecpar->codec_id = AV_CODEC_ID_NUV;
  186. vst->codecpar->width = width;
  187. vst->codecpar->height = height;
  188. vst->codecpar->bits_per_coded_sample = 10;
  189. vst->sample_aspect_ratio = av_d2q(aspect * height / width,
  190. 10000);
  191. #if FF_API_R_FRAME_RATE
  192. vst->r_frame_rate =
  193. #endif
  194. vst->avg_frame_rate = av_d2q(fps, 60000);
  195. avpriv_set_pts_info(vst, 32, 1, 1000);
  196. } else
  197. ctx->v_id = -1;
  198. if (a_packs) {
  199. ast = avformat_new_stream(s, NULL);
  200. if (!ast)
  201. return AVERROR(ENOMEM);
  202. ctx->a_id = ast->index;
  203. ast->codecpar->codec_type = AVMEDIA_TYPE_AUDIO;
  204. ast->codecpar->codec_id = AV_CODEC_ID_PCM_S16LE;
  205. ast->codecpar->channels = 2;
  206. ast->codecpar->channel_layout = AV_CH_LAYOUT_STEREO;
  207. ast->codecpar->sample_rate = 44100;
  208. ast->codecpar->bit_rate = 2 * 2 * 44100 * 8;
  209. ast->codecpar->block_align = 2 * 2;
  210. ast->codecpar->bits_per_coded_sample = 16;
  211. avpriv_set_pts_info(ast, 32, 1, 1000);
  212. } else
  213. ctx->a_id = -1;
  214. if ((ret = get_codec_data(s, pb, vst, ast, is_mythtv)) < 0)
  215. return ret;
  216. ctx->rtjpg_video = vst && vst->codecpar->codec_id == AV_CODEC_ID_NUV;
  217. return 0;
  218. }
  219. #define HDRSIZE 12
  220. static int nuv_packet(AVFormatContext *s, AVPacket *pkt)
  221. {
  222. NUVContext *ctx = s->priv_data;
  223. AVIOContext *pb = s->pb;
  224. uint8_t hdr[HDRSIZE];
  225. nuv_frametype frametype;
  226. int ret, size;
  227. while (!avio_feof(pb)) {
  228. int copyhdrsize = ctx->rtjpg_video ? HDRSIZE : 0;
  229. uint64_t pos = avio_tell(pb);
  230. ret = avio_read(pb, hdr, HDRSIZE);
  231. if (ret < HDRSIZE)
  232. return ret < 0 ? ret : AVERROR(EIO);
  233. frametype = hdr[0];
  234. size = PKTSIZE(AV_RL32(&hdr[8]));
  235. switch (frametype) {
  236. case NUV_EXTRADATA:
  237. if (!ctx->rtjpg_video) {
  238. avio_skip(pb, size);
  239. break;
  240. }
  241. case NUV_VIDEO:
  242. if (ctx->v_id < 0) {
  243. av_log(s, AV_LOG_ERROR, "Video packet in file without video stream!\n");
  244. avio_skip(pb, size);
  245. break;
  246. }
  247. ret = av_new_packet(pkt, copyhdrsize + size);
  248. if (ret < 0)
  249. return ret;
  250. pkt->pos = pos;
  251. pkt->flags |= hdr[2] == 0 ? AV_PKT_FLAG_KEY : 0;
  252. pkt->pts = AV_RL32(&hdr[4]);
  253. pkt->stream_index = ctx->v_id;
  254. memcpy(pkt->data, hdr, copyhdrsize);
  255. ret = avio_read(pb, pkt->data + copyhdrsize, size);
  256. if (ret < 0) {
  257. return ret;
  258. }
  259. if (ret < size)
  260. av_shrink_packet(pkt, copyhdrsize + ret);
  261. return 0;
  262. case NUV_AUDIO:
  263. if (ctx->a_id < 0) {
  264. av_log(s, AV_LOG_ERROR, "Audio packet in file without audio stream!\n");
  265. avio_skip(pb, size);
  266. break;
  267. }
  268. ret = av_get_packet(pb, pkt, size);
  269. pkt->flags |= AV_PKT_FLAG_KEY;
  270. pkt->pos = pos;
  271. pkt->pts = AV_RL32(&hdr[4]);
  272. pkt->stream_index = ctx->a_id;
  273. if (ret < 0)
  274. return ret;
  275. return 0;
  276. case NUV_SEEKP:
  277. // contains no data, size value is invalid
  278. break;
  279. default:
  280. avio_skip(pb, size);
  281. break;
  282. }
  283. }
  284. return AVERROR(EIO);
  285. }
  286. /**
  287. * \brief looks for the string RTjjjjjjjjjj in the stream too resync reading
  288. * \return 1 if the syncword is found 0 otherwise.
  289. */
  290. static int nuv_resync(AVFormatContext *s, int64_t pos_limit) {
  291. AVIOContext *pb = s->pb;
  292. uint32_t tag = 0;
  293. while(!avio_feof(pb) && avio_tell(pb) < pos_limit) {
  294. tag = (tag << 8) | avio_r8(pb);
  295. if (tag == MKBETAG('R','T','j','j') &&
  296. (tag = avio_rb32(pb)) == MKBETAG('j','j','j','j') &&
  297. (tag = avio_rb32(pb)) == MKBETAG('j','j','j','j'))
  298. return 1;
  299. }
  300. return 0;
  301. }
  302. /**
  303. * \brief attempts to read a timestamp from stream at the given stream position
  304. * \return timestamp if successful and AV_NOPTS_VALUE if failure
  305. */
  306. static int64_t nuv_read_dts(AVFormatContext *s, int stream_index,
  307. int64_t *ppos, int64_t pos_limit)
  308. {
  309. NUVContext *ctx = s->priv_data;
  310. AVIOContext *pb = s->pb;
  311. uint8_t hdr[HDRSIZE];
  312. nuv_frametype frametype;
  313. int size, key, idx;
  314. int64_t pos, dts;
  315. if (avio_seek(pb, *ppos, SEEK_SET) < 0)
  316. return AV_NOPTS_VALUE;
  317. if (!nuv_resync(s, pos_limit))
  318. return AV_NOPTS_VALUE;
  319. while (!avio_feof(pb) && avio_tell(pb) < pos_limit) {
  320. if (avio_read(pb, hdr, HDRSIZE) < HDRSIZE)
  321. return AV_NOPTS_VALUE;
  322. frametype = hdr[0];
  323. size = PKTSIZE(AV_RL32(&hdr[8]));
  324. switch (frametype) {
  325. case NUV_SEEKP:
  326. break;
  327. case NUV_AUDIO:
  328. case NUV_VIDEO:
  329. if (frametype == NUV_VIDEO) {
  330. idx = ctx->v_id;
  331. key = hdr[2] == 0;
  332. } else {
  333. idx = ctx->a_id;
  334. key = 1;
  335. }
  336. if (stream_index == idx) {
  337. pos = avio_tell(s->pb) - HDRSIZE;
  338. dts = AV_RL32(&hdr[4]);
  339. // TODO - add general support in av_gen_search, so it adds positions after reading timestamps
  340. av_add_index_entry(s->streams[stream_index], pos, dts, size + HDRSIZE, 0,
  341. key ? AVINDEX_KEYFRAME : 0);
  342. *ppos = pos;
  343. return dts;
  344. }
  345. default:
  346. avio_skip(pb, size);
  347. break;
  348. }
  349. }
  350. return AV_NOPTS_VALUE;
  351. }
  352. AVInputFormat ff_nuv_demuxer = {
  353. .name = "nuv",
  354. .long_name = NULL_IF_CONFIG_SMALL("NuppelVideo"),
  355. .priv_data_size = sizeof(NUVContext),
  356. .read_probe = nuv_probe,
  357. .read_header = nuv_header,
  358. .read_packet = nuv_packet,
  359. .read_timestamp = nuv_read_dts,
  360. .flags = AVFMT_GENERIC_INDEX,
  361. };