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.

241 lines
7.7KB

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