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.

320 lines
10KB

  1. /*
  2. * NuppelVideo demuxer.
  3. * Copyright (c) 2006 Reimar Doeffinger
  4. *
  5. * This file is part of Libav.
  6. *
  7. * Libav 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. * Libav 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 Libav; 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(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(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 (!pb->eof_reached) {
  69. int size, subtype;
  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 (vst->codec->extradata) {
  78. av_freep(&vst->codec->extradata);
  79. vst->codec->extradata_size = 0;
  80. }
  81. vst->codec->extradata = av_malloc(size);
  82. if (!vst->codec->extradata)
  83. return AVERROR(ENOMEM);
  84. vst->codec->extradata_size = size;
  85. avio_read(pb, vst->codec->extradata, size);
  86. size = 0;
  87. if (!myth)
  88. return 0;
  89. }
  90. break;
  91. case NUV_MYTHEXT:
  92. avio_skip(pb, 7);
  93. size = PKTSIZE(avio_rl32(pb));
  94. if (size != 128 * 4)
  95. break;
  96. avio_rl32(pb); // version
  97. if (vst) {
  98. vst->codec->codec_tag = avio_rl32(pb);
  99. vst->codec->codec_id =
  100. ff_codec_get_id(ff_codec_bmp_tags, vst->codec->codec_tag);
  101. if (vst->codec->codec_tag == MKTAG('R', 'J', 'P', 'G'))
  102. vst->codec->codec_id = AV_CODEC_ID_NUV;
  103. } else
  104. avio_skip(pb, 4);
  105. if (ast) {
  106. int id;
  107. ast->codec->codec_tag = avio_rl32(pb);
  108. ast->codec->sample_rate = avio_rl32(pb);
  109. ast->codec->bits_per_coded_sample = avio_rl32(pb);
  110. ast->codec->channels = avio_rl32(pb);
  111. ast->codec->channel_layout = 0;
  112. id = ff_wav_codec_get_id(ast->codec->codec_tag,
  113. ast->codec->bits_per_coded_sample);
  114. if (id == AV_CODEC_ID_NONE) {
  115. id = ff_codec_get_id(nuv_audio_tags, ast->codec->codec_tag);
  116. if (id == AV_CODEC_ID_PCM_S16LE)
  117. id = ff_get_pcm_codec_id(ast->codec->bits_per_coded_sample,
  118. 0, 0, ~1);
  119. }
  120. ast->codec->codec_id = id;
  121. ast->need_parsing = AVSTREAM_PARSE_FULL;
  122. } else
  123. avio_skip(pb, 4 * 4);
  124. size -= 6 * 4;
  125. avio_skip(pb, size);
  126. return 0;
  127. case NUV_SEEKP:
  128. size = 11;
  129. break;
  130. default:
  131. avio_skip(pb, 7);
  132. size = PKTSIZE(avio_rl32(pb));
  133. break;
  134. }
  135. avio_skip(pb, size);
  136. }
  137. return 0;
  138. }
  139. static int nuv_header(AVFormatContext *s)
  140. {
  141. NUVContext *ctx = s->priv_data;
  142. AVIOContext *pb = s->pb;
  143. char id_string[12];
  144. double aspect, fps;
  145. int is_mythtv, width, height, v_packs, a_packs, ret;
  146. AVStream *vst = NULL, *ast = NULL;
  147. avio_read(pb, id_string, 12);
  148. is_mythtv = !memcmp(id_string, "MythTVVideo", 12);
  149. avio_skip(pb, 5); // version string
  150. avio_skip(pb, 3); // padding
  151. width = avio_rl32(pb);
  152. height = avio_rl32(pb);
  153. avio_rl32(pb); // unused, "desiredwidth"
  154. avio_rl32(pb); // unused, "desiredheight"
  155. avio_r8(pb); // 'P' == progressive, 'I' == interlaced
  156. avio_skip(pb, 3); // padding
  157. aspect = av_int2double(avio_rl64(pb));
  158. if (aspect > 0.9999 && aspect < 1.0001)
  159. aspect = 4.0 / 3.0;
  160. fps = av_int2double(avio_rl64(pb));
  161. // number of packets per stream type, -1 means unknown, e.g. streaming
  162. v_packs = avio_rl32(pb);
  163. a_packs = avio_rl32(pb);
  164. avio_rl32(pb); // text
  165. avio_rl32(pb); // keyframe distance (?)
  166. if (v_packs) {
  167. vst = avformat_new_stream(s, NULL);
  168. if (!vst)
  169. return AVERROR(ENOMEM);
  170. ctx->v_id = vst->index;
  171. ret = av_image_check_size(width, height, 0, ctx);
  172. if (ret < 0)
  173. return ret;
  174. vst->codec->codec_type = AVMEDIA_TYPE_VIDEO;
  175. vst->codec->codec_id = AV_CODEC_ID_NUV;
  176. vst->codec->width = width;
  177. vst->codec->height = height;
  178. vst->codec->bits_per_coded_sample = 10;
  179. vst->sample_aspect_ratio = av_d2q(aspect * height / width,
  180. 10000);
  181. vst->avg_frame_rate = av_d2q(fps, 60000);
  182. avpriv_set_pts_info(vst, 32, 1, 1000);
  183. } else
  184. ctx->v_id = -1;
  185. if (a_packs) {
  186. ast = avformat_new_stream(s, NULL);
  187. if (!ast)
  188. return AVERROR(ENOMEM);
  189. ctx->a_id = ast->index;
  190. ast->codec->codec_type = AVMEDIA_TYPE_AUDIO;
  191. ast->codec->codec_id = AV_CODEC_ID_PCM_S16LE;
  192. ast->codec->channels = 2;
  193. ast->codec->channel_layout = AV_CH_LAYOUT_STEREO;
  194. ast->codec->sample_rate = 44100;
  195. ast->codec->bit_rate = 2 * 2 * 44100 * 8;
  196. ast->codec->block_align = 2 * 2;
  197. ast->codec->bits_per_coded_sample = 16;
  198. avpriv_set_pts_info(ast, 32, 1, 1000);
  199. } else
  200. ctx->a_id = -1;
  201. if ((ret = get_codec_data(pb, vst, ast, is_mythtv)) < 0)
  202. return ret;
  203. ctx->rtjpg_video = vst && vst->codec->codec_id == AV_CODEC_ID_NUV;
  204. return 0;
  205. }
  206. #define HDRSIZE 12
  207. static int nuv_packet(AVFormatContext *s, AVPacket *pkt)
  208. {
  209. NUVContext *ctx = s->priv_data;
  210. AVIOContext *pb = s->pb;
  211. uint8_t hdr[HDRSIZE];
  212. nuv_frametype frametype;
  213. int ret, size;
  214. while (!pb->eof_reached) {
  215. int copyhdrsize = ctx->rtjpg_video ? HDRSIZE : 0;
  216. uint64_t pos = avio_tell(pb);
  217. ret = avio_read(pb, hdr, HDRSIZE);
  218. if (ret < HDRSIZE)
  219. return ret < 0 ? ret : AVERROR(EIO);
  220. frametype = hdr[0];
  221. size = PKTSIZE(AV_RL32(&hdr[8]));
  222. switch (frametype) {
  223. case NUV_EXTRADATA:
  224. if (!ctx->rtjpg_video) {
  225. avio_skip(pb, size);
  226. break;
  227. }
  228. case NUV_VIDEO:
  229. if (ctx->v_id < 0) {
  230. av_log(s, AV_LOG_ERROR, "Video packet in file without video stream!\n");
  231. avio_skip(pb, size);
  232. break;
  233. }
  234. ret = av_new_packet(pkt, copyhdrsize + size);
  235. if (ret < 0)
  236. return ret;
  237. // HACK: we have no idea if it is a keyframe,
  238. // but if we mark none seeking will not work at all.
  239. pkt->flags |= AV_PKT_FLAG_KEY;
  240. pkt->pos = pos;
  241. pkt->pts = AV_RL32(&hdr[4]);
  242. pkt->stream_index = ctx->v_id;
  243. memcpy(pkt->data, hdr, copyhdrsize);
  244. ret = avio_read(pb, pkt->data + copyhdrsize, size);
  245. if (ret < 0) {
  246. av_free_packet(pkt);
  247. return ret;
  248. }
  249. if (ret < size)
  250. av_shrink_packet(pkt, copyhdrsize + ret);
  251. return 0;
  252. case NUV_AUDIO:
  253. if (ctx->a_id < 0) {
  254. av_log(s, AV_LOG_ERROR, "Audio packet in file without audio stream!\n");
  255. avio_skip(pb, size);
  256. break;
  257. }
  258. ret = av_get_packet(pb, pkt, size);
  259. pkt->flags |= AV_PKT_FLAG_KEY;
  260. pkt->pos = pos;
  261. pkt->pts = AV_RL32(&hdr[4]);
  262. pkt->stream_index = ctx->a_id;
  263. if (ret < 0)
  264. return ret;
  265. return 0;
  266. case NUV_SEEKP:
  267. // contains no data, size value is invalid
  268. break;
  269. default:
  270. avio_skip(pb, size);
  271. break;
  272. }
  273. }
  274. return AVERROR(EIO);
  275. }
  276. AVInputFormat ff_nuv_demuxer = {
  277. .name = "nuv",
  278. .long_name = NULL_IF_CONFIG_SMALL("NuppelVideo"),
  279. .priv_data_size = sizeof(NUVContext),
  280. .read_probe = nuv_probe,
  281. .read_header = nuv_header,
  282. .read_packet = nuv_packet,
  283. .flags = AVFMT_GENERIC_INDEX,
  284. };