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.

246 lines
7.8KB

  1. /*
  2. * NuppelVideo demuxer.
  3. * Copyright (c) 2006 Reimar Doeffinger.
  4. *
  5. * This library is free software; you can redistribute it and/or
  6. * modify it under the terms of the GNU Lesser General Public
  7. * License as published by the Free Software Foundation; either
  8. * version 2 of the License, or (at your option) any later version.
  9. *
  10. * This library is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  13. * Lesser General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU Lesser General Public
  16. * License along with this library; if not, write to the Free Software
  17. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  18. */
  19. #include "avformat.h"
  20. #include "avi.h"
  21. typedef struct {
  22. int v_id;
  23. int a_id;
  24. } NUVContext;
  25. typedef enum {
  26. NUV_VIDEO = 'V',
  27. NUV_EXTRADATA = 'D',
  28. NUV_AUDIO = 'A',
  29. NUV_SEEKP = 'R',
  30. NUV_MYTHEXT = 'X'
  31. } frametype_t;
  32. static int nuv_probe(AVProbeData *p) {
  33. if (p->buf_size < 12)
  34. return 0;
  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 (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. } else
  93. url_fskip(pb, 4 * 4);
  94. size -= 6 * 4;
  95. url_fskip(pb, size);
  96. return 1;
  97. case NUV_SEEKP:
  98. size = 11;
  99. break;
  100. default:
  101. url_fskip(pb, 7);
  102. size = PKTSIZE(get_le32(pb));
  103. break;
  104. }
  105. url_fskip(pb, size);
  106. }
  107. return 0;
  108. }
  109. static int nuv_header(AVFormatContext *s, AVFormatParameters *ap) {
  110. NUVContext *ctx = (NUVContext *)s->priv_data;
  111. ByteIOContext *pb = &s->pb;
  112. char id_string[12], version_string[5];
  113. double aspect, fps;
  114. int is_mythtv, width, height, v_packs, a_packs;
  115. int stream_nr = 0;
  116. AVStream *vst = NULL, *ast = NULL;
  117. get_buffer(pb, id_string, 12);
  118. is_mythtv = !memcmp(id_string, "MythTVVideo", 12);
  119. get_buffer(pb, version_string, 5);
  120. url_fskip(pb, 3); // padding
  121. width = get_le32(pb);
  122. height = get_le32(pb);
  123. get_le32(pb); // unused, "desiredwidth"
  124. get_le32(pb); // unused, "desiredheight"
  125. get_byte(pb); // 'P' == progressive, 'I' == interlaced
  126. url_fskip(pb, 3); // padding
  127. aspect = av_int2dbl(get_le64(pb));
  128. fps = av_int2dbl(get_le64(pb));
  129. // number of packets per stream type, -1 means unknown, e.g. streaming
  130. v_packs = get_le32(pb);
  131. a_packs = get_le32(pb);
  132. get_le32(pb); // text
  133. get_le32(pb); // keyframe distance (?)
  134. if (v_packs) {
  135. ctx->v_id = stream_nr++;
  136. vst = av_new_stream(s, ctx->v_id);
  137. vst->codec->codec_type = CODEC_TYPE_VIDEO;
  138. vst->codec->codec_id = CODEC_ID_NUV;
  139. vst->codec->codec_tag = MKTAG('R', 'J', 'P', 'G');
  140. vst->codec->width = width;
  141. vst->codec->height = height;
  142. vst->codec->bits_per_sample = 10;
  143. vst->codec->sample_aspect_ratio = av_d2q(aspect, 10000);
  144. vst->r_frame_rate = av_d2q(1.0 / fps, 10000);
  145. av_set_pts_info(vst, 32, 1, 1000);
  146. } else
  147. ctx->v_id = -1;
  148. if (a_packs) {
  149. ctx->a_id = stream_nr++;
  150. ast = av_new_stream(s, ctx->a_id);
  151. ast->codec->codec_type = CODEC_TYPE_AUDIO;
  152. ast->codec->codec_id = CODEC_ID_PCM_S16LE;
  153. ast->codec->channels = 2;
  154. ast->codec->sample_rate = 44100;
  155. ast->codec->bit_rate = 2 * 2 * 44100 * 8;
  156. ast->codec->block_align = 2 * 2;
  157. ast->codec->bits_per_sample = 16;
  158. av_set_pts_info(ast, 32, 1, 1000);
  159. } else
  160. ctx->a_id = -1;
  161. get_codec_data(pb, vst, ast, is_mythtv);
  162. return 0;
  163. }
  164. #define HDRSIZE 12
  165. static int nuv_packet(AVFormatContext *s, AVPacket *pkt) {
  166. NUVContext *ctx = (NUVContext *)s->priv_data;
  167. ByteIOContext *pb = &s->pb;
  168. uint8_t hdr[HDRSIZE];
  169. frametype_t frametype;
  170. int ret, size;
  171. while (!url_feof(pb)) {
  172. ret = get_buffer(pb, hdr, HDRSIZE);
  173. if (ret <= 0)
  174. return ret;
  175. frametype = hdr[0];
  176. size = PKTSIZE(LE_32(&hdr[8]));
  177. switch (frametype) {
  178. case NUV_VIDEO:
  179. case NUV_EXTRADATA:
  180. if (ctx->v_id < 0) {
  181. av_log(s, AV_LOG_ERROR, "Video packet in file without video stream!\n");
  182. url_fskip(pb, size);
  183. break;
  184. }
  185. ret = av_new_packet(pkt, HDRSIZE + size);
  186. if (ret < 0)
  187. return ret;
  188. pkt->pos = url_ftell(pb);
  189. pkt->pts = LE_32(&hdr[4]);
  190. pkt->stream_index = ctx->v_id;
  191. memcpy(pkt->data, hdr, HDRSIZE);
  192. ret = get_buffer(pb, pkt->data + HDRSIZE, size);
  193. return ret;
  194. case NUV_AUDIO:
  195. if (ctx->a_id < 0) {
  196. av_log(s, AV_LOG_ERROR, "Audio packet in file without audio stream!\n");
  197. url_fskip(pb, size);
  198. break;
  199. }
  200. ret = av_get_packet(pb, pkt, size);
  201. pkt->pts = LE_32(&hdr[4]);
  202. pkt->stream_index = ctx->a_id;
  203. return ret;
  204. case NUV_SEEKP:
  205. // contains no data, size value is invalid
  206. break;
  207. default:
  208. url_fskip(pb, size);
  209. break;
  210. }
  211. }
  212. return AVERROR_IO;
  213. }
  214. static AVInputFormat nuv_iformat = {
  215. "nuv",
  216. "NuppelVideo format",
  217. sizeof(NUVContext),
  218. nuv_probe,
  219. nuv_header,
  220. nuv_packet,
  221. NULL,
  222. NULL,
  223. };
  224. int nuv_init(void) {
  225. av_register_input_format(&nuv_iformat);
  226. return 0;
  227. }