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.

255 lines
8.3KB

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