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.

318 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/intreadwrite.h"
  23. #include "libavutil/intfloat.h"
  24. #include "avformat.h"
  25. #include "internal.h"
  26. #include "riff.h"
  27. static const AVCodecTag nuv_audio_tags[] = {
  28. { AV_CODEC_ID_PCM_S16LE, MKTAG('R', 'A', 'W', 'A') },
  29. { AV_CODEC_ID_MP3, MKTAG('L', 'A', 'M', 'E') },
  30. { AV_CODEC_ID_NONE, 0 },
  31. };
  32. typedef struct {
  33. int v_id;
  34. int a_id;
  35. int rtjpg_video;
  36. } NUVContext;
  37. typedef enum {
  38. NUV_VIDEO = 'V',
  39. NUV_EXTRADATA = 'D',
  40. NUV_AUDIO = 'A',
  41. NUV_SEEKP = 'R',
  42. NUV_MYTHEXT = 'X'
  43. } nuv_frametype;
  44. static int nuv_probe(AVProbeData *p)
  45. {
  46. if (!memcmp(p->buf, "NuppelVideo", 12))
  47. return AVPROBE_SCORE_MAX;
  48. if (!memcmp(p->buf, "MythTVVideo", 12))
  49. return AVPROBE_SCORE_MAX;
  50. return 0;
  51. }
  52. /// little macro to sanitize packet size
  53. #define PKTSIZE(s) (s & 0xffffff)
  54. /**
  55. * @brief read until we found all data needed for decoding
  56. * @param vst video stream of which to change parameters
  57. * @param ast video stream of which to change parameters
  58. * @param myth set if this is a MythTVVideo format file
  59. * @return 0 or AVERROR code
  60. */
  61. static int get_codec_data(AVIOContext *pb, AVStream *vst,
  62. AVStream *ast, int myth)
  63. {
  64. nuv_frametype frametype;
  65. if (!vst && !myth)
  66. return 1; // no codec data needed
  67. while (!pb->eof_reached) {
  68. int size, subtype;
  69. frametype = avio_r8(pb);
  70. switch (frametype) {
  71. case NUV_EXTRADATA:
  72. subtype = avio_r8(pb);
  73. avio_skip(pb, 6);
  74. size = PKTSIZE(avio_rl32(pb));
  75. if (vst && subtype == 'R') {
  76. if (vst->codec->extradata) {
  77. av_freep(&vst->codec->extradata);
  78. vst->codec->extradata_size = 0;
  79. }
  80. vst->codec->extradata = av_malloc(size);
  81. if (!vst->codec->extradata)
  82. return AVERROR(ENOMEM);
  83. vst->codec->extradata_size = size;
  84. avio_read(pb, vst->codec->extradata, size);
  85. size = 0;
  86. if (!myth)
  87. return 0;
  88. }
  89. break;
  90. case NUV_MYTHEXT:
  91. avio_skip(pb, 7);
  92. size = PKTSIZE(avio_rl32(pb));
  93. if (size != 128 * 4)
  94. break;
  95. avio_rl32(pb); // version
  96. if (vst) {
  97. vst->codec->codec_tag = avio_rl32(pb);
  98. vst->codec->codec_id =
  99. ff_codec_get_id(ff_codec_bmp_tags, vst->codec->codec_tag);
  100. if (vst->codec->codec_tag == MKTAG('R', 'J', 'P', 'G'))
  101. vst->codec->codec_id = AV_CODEC_ID_NUV;
  102. } else
  103. avio_skip(pb, 4);
  104. if (ast) {
  105. int id;
  106. ast->codec->codec_tag = avio_rl32(pb);
  107. ast->codec->sample_rate = avio_rl32(pb);
  108. ast->codec->bits_per_coded_sample = avio_rl32(pb);
  109. ast->codec->channels = avio_rl32(pb);
  110. ast->codec->channel_layout = 0;
  111. id = ff_wav_codec_get_id(ast->codec->codec_tag,
  112. ast->codec->bits_per_coded_sample);
  113. if (id == AV_CODEC_ID_NONE) {
  114. id = ff_codec_get_id(nuv_audio_tags, ast->codec->codec_tag);
  115. if (id == AV_CODEC_ID_PCM_S16LE)
  116. id = ff_get_pcm_codec_id(ast->codec->bits_per_coded_sample,
  117. 0, 0, ~1);
  118. }
  119. ast->codec->codec_id = id;
  120. ast->need_parsing = AVSTREAM_PARSE_FULL;
  121. } else
  122. avio_skip(pb, 4 * 4);
  123. size -= 6 * 4;
  124. avio_skip(pb, size);
  125. return 0;
  126. case NUV_SEEKP:
  127. size = 11;
  128. break;
  129. default:
  130. avio_skip(pb, 7);
  131. size = PKTSIZE(avio_rl32(pb));
  132. break;
  133. }
  134. avio_skip(pb, size);
  135. }
  136. return 0;
  137. }
  138. static int nuv_header(AVFormatContext *s)
  139. {
  140. NUVContext *ctx = s->priv_data;
  141. AVIOContext *pb = s->pb;
  142. char id_string[12];
  143. double aspect, fps;
  144. int is_mythtv, width, height, v_packs, a_packs, ret;
  145. AVStream *vst = NULL, *ast = NULL;
  146. avio_read(pb, id_string, 12);
  147. is_mythtv = !memcmp(id_string, "MythTVVideo", 12);
  148. avio_skip(pb, 5); // version string
  149. avio_skip(pb, 3); // padding
  150. width = avio_rl32(pb);
  151. height = avio_rl32(pb);
  152. avio_rl32(pb); // unused, "desiredwidth"
  153. avio_rl32(pb); // unused, "desiredheight"
  154. avio_r8(pb); // 'P' == progressive, 'I' == interlaced
  155. avio_skip(pb, 3); // padding
  156. aspect = av_int2double(avio_rl64(pb));
  157. if (aspect > 0.9999 && aspect < 1.0001)
  158. aspect = 4.0 / 3.0;
  159. fps = av_int2double(avio_rl64(pb));
  160. // number of packets per stream type, -1 means unknown, e.g. streaming
  161. v_packs = avio_rl32(pb);
  162. a_packs = avio_rl32(pb);
  163. avio_rl32(pb); // text
  164. avio_rl32(pb); // keyframe distance (?)
  165. if (v_packs) {
  166. vst = avformat_new_stream(s, NULL);
  167. if (!vst)
  168. return AVERROR(ENOMEM);
  169. ctx->v_id = vst->index;
  170. vst->codec->codec_type = AVMEDIA_TYPE_VIDEO;
  171. vst->codec->codec_id = AV_CODEC_ID_NUV;
  172. vst->codec->width = width;
  173. vst->codec->height = height;
  174. vst->codec->bits_per_coded_sample = 10;
  175. vst->sample_aspect_ratio = av_d2q(aspect * height / width,
  176. 10000);
  177. #if FF_API_R_FRAME_RATE
  178. vst->r_frame_rate =
  179. #endif
  180. vst->avg_frame_rate = av_d2q(fps, 60000);
  181. avpriv_set_pts_info(vst, 32, 1, 1000);
  182. } else
  183. ctx->v_id = -1;
  184. if (a_packs) {
  185. ast = avformat_new_stream(s, NULL);
  186. if (!ast)
  187. return AVERROR(ENOMEM);
  188. ctx->a_id = ast->index;
  189. ast->codec->codec_type = AVMEDIA_TYPE_AUDIO;
  190. ast->codec->codec_id = AV_CODEC_ID_PCM_S16LE;
  191. ast->codec->channels = 2;
  192. ast->codec->channel_layout = AV_CH_LAYOUT_STEREO;
  193. ast->codec->sample_rate = 44100;
  194. ast->codec->bit_rate = 2 * 2 * 44100 * 8;
  195. ast->codec->block_align = 2 * 2;
  196. ast->codec->bits_per_coded_sample = 16;
  197. avpriv_set_pts_info(ast, 32, 1, 1000);
  198. } else
  199. ctx->a_id = -1;
  200. if ((ret = get_codec_data(pb, vst, ast, is_mythtv)) < 0)
  201. return ret;
  202. ctx->rtjpg_video = vst && vst->codec->codec_id == AV_CODEC_ID_NUV;
  203. return 0;
  204. }
  205. #define HDRSIZE 12
  206. static int nuv_packet(AVFormatContext *s, AVPacket *pkt)
  207. {
  208. NUVContext *ctx = s->priv_data;
  209. AVIOContext *pb = s->pb;
  210. uint8_t hdr[HDRSIZE];
  211. nuv_frametype frametype;
  212. int ret, size;
  213. while (!pb->eof_reached) {
  214. int copyhdrsize = ctx->rtjpg_video ? HDRSIZE : 0;
  215. uint64_t pos = avio_tell(pb);
  216. ret = avio_read(pb, hdr, HDRSIZE);
  217. if (ret < HDRSIZE)
  218. return ret < 0 ? ret : AVERROR(EIO);
  219. frametype = hdr[0];
  220. size = PKTSIZE(AV_RL32(&hdr[8]));
  221. switch (frametype) {
  222. case NUV_EXTRADATA:
  223. if (!ctx->rtjpg_video) {
  224. avio_skip(pb, size);
  225. break;
  226. }
  227. case NUV_VIDEO:
  228. if (ctx->v_id < 0) {
  229. av_log(s, AV_LOG_ERROR, "Video packet in file without video stream!\n");
  230. avio_skip(pb, size);
  231. break;
  232. }
  233. ret = av_new_packet(pkt, copyhdrsize + size);
  234. if (ret < 0)
  235. return ret;
  236. // HACK: we have no idea if it is a keyframe,
  237. // but if we mark none seeking will not work at all.
  238. pkt->flags |= AV_PKT_FLAG_KEY;
  239. pkt->pos = pos;
  240. pkt->pts = AV_RL32(&hdr[4]);
  241. pkt->stream_index = ctx->v_id;
  242. memcpy(pkt->data, hdr, copyhdrsize);
  243. ret = avio_read(pb, pkt->data + copyhdrsize, size);
  244. if (ret < 0) {
  245. av_free_packet(pkt);
  246. return ret;
  247. }
  248. if (ret < size)
  249. av_shrink_packet(pkt, copyhdrsize + ret);
  250. return 0;
  251. case NUV_AUDIO:
  252. if (ctx->a_id < 0) {
  253. av_log(s, AV_LOG_ERROR, "Audio packet in file without audio stream!\n");
  254. avio_skip(pb, size);
  255. break;
  256. }
  257. ret = av_get_packet(pb, pkt, size);
  258. pkt->flags |= AV_PKT_FLAG_KEY;
  259. pkt->pos = pos;
  260. pkt->pts = AV_RL32(&hdr[4]);
  261. pkt->stream_index = ctx->a_id;
  262. if (ret < 0)
  263. return ret;
  264. return 0;
  265. case NUV_SEEKP:
  266. // contains no data, size value is invalid
  267. break;
  268. default:
  269. avio_skip(pb, size);
  270. break;
  271. }
  272. }
  273. return AVERROR(EIO);
  274. }
  275. AVInputFormat ff_nuv_demuxer = {
  276. .name = "nuv",
  277. .long_name = NULL_IF_CONFIG_SMALL("NuppelVideo"),
  278. .priv_data_size = sizeof(NUVContext),
  279. .read_probe = nuv_probe,
  280. .read_header = nuv_header,
  281. .read_packet = nuv_packet,
  282. .flags = AVFMT_GENERIC_INDEX,
  283. };