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.

249 lines
8.1KB

  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. } frametype_t;
  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. frametype_t 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_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_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. fps = av_int2dbl(get_le64(pb));
  133. // number of packets per stream type, -1 means unknown, e.g. streaming
  134. v_packs = get_le32(pb);
  135. a_packs = get_le32(pb);
  136. get_le32(pb); // text
  137. get_le32(pb); // keyframe distance (?)
  138. if (v_packs) {
  139. ctx->v_id = stream_nr++;
  140. vst = av_new_stream(s, ctx->v_id);
  141. vst->codec->codec_type = CODEC_TYPE_VIDEO;
  142. vst->codec->codec_id = CODEC_ID_NUV;
  143. vst->codec->width = width;
  144. vst->codec->height = height;
  145. vst->codec->bits_per_sample = 10;
  146. vst->codec->sample_aspect_ratio = av_d2q(aspect, 10000);
  147. vst->r_frame_rate = av_d2q(fps, 60000);
  148. av_set_pts_info(vst, 32, 1, 1000);
  149. } else
  150. ctx->v_id = -1;
  151. if (a_packs) {
  152. ctx->a_id = stream_nr++;
  153. ast = av_new_stream(s, ctx->a_id);
  154. ast->codec->codec_type = CODEC_TYPE_AUDIO;
  155. ast->codec->codec_id = CODEC_ID_PCM_S16LE;
  156. ast->codec->channels = 2;
  157. ast->codec->sample_rate = 44100;
  158. ast->codec->bit_rate = 2 * 2 * 44100 * 8;
  159. ast->codec->block_align = 2 * 2;
  160. ast->codec->bits_per_sample = 16;
  161. av_set_pts_info(ast, 32, 1, 1000);
  162. } else
  163. ctx->a_id = -1;
  164. get_codec_data(pb, vst, ast, is_mythtv);
  165. ctx->rtjpg_video = vst->codec->codec_id == CODEC_ID_NUV;
  166. return 0;
  167. }
  168. #define HDRSIZE 12
  169. static int nuv_packet(AVFormatContext *s, AVPacket *pkt) {
  170. NUVContext *ctx = s->priv_data;
  171. ByteIOContext *pb = s->pb;
  172. uint8_t hdr[HDRSIZE];
  173. frametype_t frametype;
  174. int ret, size;
  175. while (!url_feof(pb)) {
  176. int copyhdrsize = ctx->rtjpg_video ? HDRSIZE : 0;
  177. ret = get_buffer(pb, hdr, HDRSIZE);
  178. if (ret <= 0)
  179. return ret ? ret : -1;
  180. frametype = hdr[0];
  181. size = PKTSIZE(AV_RL32(&hdr[8]));
  182. switch (frametype) {
  183. case NUV_EXTRADATA:
  184. if (!ctx->rtjpg_video) {
  185. url_fskip(pb, size);
  186. break;
  187. }
  188. case NUV_VIDEO:
  189. if (ctx->v_id < 0) {
  190. av_log(s, AV_LOG_ERROR, "Video packet in file without video stream!\n");
  191. url_fskip(pb, size);
  192. break;
  193. }
  194. ret = av_new_packet(pkt, copyhdrsize + size);
  195. if (ret < 0)
  196. return ret;
  197. pkt->pos = url_ftell(pb) - copyhdrsize;
  198. pkt->pts = AV_RL32(&hdr[4]);
  199. pkt->stream_index = ctx->v_id;
  200. memcpy(pkt->data, hdr, copyhdrsize);
  201. ret = get_buffer(pb, pkt->data + copyhdrsize, size);
  202. return ret;
  203. case NUV_AUDIO:
  204. if (ctx->a_id < 0) {
  205. av_log(s, AV_LOG_ERROR, "Audio packet in file without audio stream!\n");
  206. url_fskip(pb, size);
  207. break;
  208. }
  209. ret = av_get_packet(pb, pkt, size);
  210. pkt->pts = AV_RL32(&hdr[4]);
  211. pkt->stream_index = ctx->a_id;
  212. return ret;
  213. case NUV_SEEKP:
  214. // contains no data, size value is invalid
  215. break;
  216. default:
  217. url_fskip(pb, size);
  218. break;
  219. }
  220. }
  221. return AVERROR(EIO);
  222. }
  223. AVInputFormat nuv_demuxer = {
  224. "nuv",
  225. "NuppelVideo format",
  226. sizeof(NUVContext),
  227. nuv_probe,
  228. nuv_header,
  229. nuv_packet,
  230. NULL,
  231. NULL,
  232. };