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.

271 lines
9.1KB

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