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.

312 lines
9.4KB

  1. /*
  2. * NUT (de)muxing via libnut
  3. * copyright (c) 2006 Oded Shimon <ods15@ods15.dyndns.org>
  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. /**
  22. * @file
  23. * NUT demuxing and muxing via libnut.
  24. * @author Oded Shimon <ods15@ods15.dyndns.org>
  25. */
  26. #include "avformat.h"
  27. #include "internal.h"
  28. #include "riff.h"
  29. #include <libnut.h>
  30. #define ID_STRING "nut/multimedia container"
  31. #define ID_LENGTH (strlen(ID_STRING) + 1)
  32. typedef struct {
  33. nut_context_tt * nut;
  34. nut_stream_header_tt * s;
  35. } NUTContext;
  36. static const AVCodecTag nut_tags[] = {
  37. { CODEC_ID_MPEG4, MKTAG('m', 'p', '4', 'v') },
  38. { CODEC_ID_MP3, MKTAG('m', 'p', '3', ' ') },
  39. { CODEC_ID_VORBIS, MKTAG('v', 'r', 'b', 's') },
  40. { 0, 0 },
  41. };
  42. #if CONFIG_LIBNUT_MUXER
  43. static int av_write(void * h, size_t len, const uint8_t * buf) {
  44. AVIOContext * bc = h;
  45. avio_write(bc, buf, len);
  46. //avio_flush(bc);
  47. return len;
  48. }
  49. static int nut_write_header(AVFormatContext * avf) {
  50. NUTContext * priv = avf->priv_data;
  51. AVIOContext * bc = avf->pb;
  52. nut_muxer_opts_tt mopts = {
  53. .output = {
  54. .priv = bc,
  55. .write = av_write,
  56. },
  57. .alloc = { av_malloc, av_realloc, av_free },
  58. .write_index = 1,
  59. .realtime_stream = 0,
  60. .max_distance = 32768,
  61. .fti = NULL,
  62. };
  63. nut_stream_header_tt * s;
  64. int i;
  65. priv->s = s = av_mallocz((avf->nb_streams + 1) * sizeof*s);
  66. for (i = 0; i < avf->nb_streams; i++) {
  67. AVCodecContext * codec = avf->streams[i]->codec;
  68. int j;
  69. int fourcc = 0;
  70. int num, denom, ssize;
  71. s[i].type = codec->codec_type == AVMEDIA_TYPE_VIDEO ? NUT_VIDEO_CLASS : NUT_AUDIO_CLASS;
  72. if (codec->codec_tag) fourcc = codec->codec_tag;
  73. else fourcc = ff_codec_get_tag(nut_tags, codec->codec_id);
  74. if (!fourcc) {
  75. if (codec->codec_type == AVMEDIA_TYPE_VIDEO) fourcc = ff_codec_get_tag(ff_codec_bmp_tags, codec->codec_id);
  76. if (codec->codec_type == AVMEDIA_TYPE_AUDIO) fourcc = ff_codec_get_tag(ff_codec_wav_tags, codec->codec_id);
  77. }
  78. s[i].fourcc_len = 4;
  79. s[i].fourcc = av_malloc(s[i].fourcc_len);
  80. for (j = 0; j < s[i].fourcc_len; j++) s[i].fourcc[j] = (fourcc >> (j*8)) & 0xFF;
  81. ff_parse_specific_params(codec, &num, &ssize, &denom);
  82. avpriv_set_pts_info(avf->streams[i], 60, denom, num);
  83. s[i].time_base.num = denom;
  84. s[i].time_base.den = num;
  85. s[i].fixed_fps = 0;
  86. s[i].decode_delay = codec->has_b_frames;
  87. s[i].codec_specific_len = codec->extradata_size;
  88. s[i].codec_specific = codec->extradata;
  89. if (codec->codec_type == AVMEDIA_TYPE_VIDEO) {
  90. s[i].width = codec->width;
  91. s[i].height = codec->height;
  92. s[i].sample_width = 0;
  93. s[i].sample_height = 0;
  94. s[i].colorspace_type = 0;
  95. } else {
  96. s[i].samplerate_num = codec->sample_rate;
  97. s[i].samplerate_denom = 1;
  98. s[i].channel_count = codec->channels;
  99. }
  100. }
  101. s[avf->nb_streams].type = -1;
  102. priv->nut = nut_muxer_init(&mopts, s, NULL);
  103. return 0;
  104. }
  105. static int nut_write_packet(AVFormatContext * avf, AVPacket * pkt) {
  106. NUTContext * priv = avf->priv_data;
  107. nut_packet_tt p;
  108. p.len = pkt->size;
  109. p.stream = pkt->stream_index;
  110. p.pts = pkt->pts;
  111. p.flags = pkt->flags & AV_PKT_FLAG_KEY ? NUT_FLAG_KEY : 0;
  112. p.next_pts = 0;
  113. nut_write_frame_reorder(priv->nut, &p, pkt->data);
  114. return 0;
  115. }
  116. static int nut_write_trailer(AVFormatContext * avf) {
  117. AVIOContext * bc = avf->pb;
  118. NUTContext * priv = avf->priv_data;
  119. int i;
  120. nut_muxer_uninit_reorder(priv->nut);
  121. avio_flush(bc);
  122. for(i = 0; priv->s[i].type != -1; i++ ) av_freep(&priv->s[i].fourcc);
  123. av_freep(&priv->s);
  124. return 0;
  125. }
  126. AVOutputFormat ff_libnut_muxer = {
  127. .name = "libnut",
  128. .long_name = "nut format",
  129. .mime_type = "video/x-nut",
  130. .extensions = "nut",
  131. .priv_data_size = sizeof(NUTContext),
  132. .audio_codec = CODEC_ID_VORBIS,
  133. .video_codec = CODEC_ID_MPEG4,
  134. .write_header = nut_write_header,
  135. .write_packet = nut_write_packet,
  136. .write_trailer = nut_write_trailer,
  137. .flags = AVFMT_GLOBALHEADER,
  138. };
  139. #endif /* CONFIG_LIBNUT_MUXER */
  140. static int nut_probe(AVProbeData *p) {
  141. if (!memcmp(p->buf, ID_STRING, ID_LENGTH)) return AVPROBE_SCORE_MAX;
  142. return 0;
  143. }
  144. static size_t av_read(void * h, size_t len, uint8_t * buf) {
  145. AVIOContext * bc = h;
  146. return avio_read(bc, buf, len);
  147. }
  148. static off_t av_seek(void * h, long long pos, int whence) {
  149. AVIOContext * bc = h;
  150. if (whence == SEEK_END) {
  151. pos = avio_size(bc) + pos;
  152. whence = SEEK_SET;
  153. }
  154. return avio_seek(bc, pos, whence);
  155. }
  156. static int nut_read_header(AVFormatContext * avf) {
  157. NUTContext * priv = avf->priv_data;
  158. AVIOContext * bc = avf->pb;
  159. nut_demuxer_opts_tt dopts = {
  160. .input = {
  161. .priv = bc,
  162. .seek = av_seek,
  163. .read = av_read,
  164. .eof = NULL,
  165. .file_pos = 0,
  166. },
  167. .alloc = { av_malloc, av_realloc, av_free },
  168. .read_index = 1,
  169. .cache_syncpoints = 1,
  170. };
  171. nut_context_tt * nut = priv->nut = nut_demuxer_init(&dopts);
  172. nut_stream_header_tt * s;
  173. int ret, i;
  174. if ((ret = nut_read_headers(nut, &s, NULL))) {
  175. av_log(avf, AV_LOG_ERROR, " NUT error: %s\n", nut_error(ret));
  176. nut_demuxer_uninit(nut);
  177. return -1;
  178. }
  179. priv->s = s;
  180. for (i = 0; s[i].type != -1 && i < 2; i++) {
  181. AVStream * st = avformat_new_stream(avf, NULL);
  182. int j;
  183. for (j = 0; j < s[i].fourcc_len && j < 8; j++) st->codec->codec_tag |= s[i].fourcc[j]<<(j*8);
  184. st->codec->has_b_frames = s[i].decode_delay;
  185. st->codec->extradata_size = s[i].codec_specific_len;
  186. if (st->codec->extradata_size) {
  187. st->codec->extradata = av_mallocz(st->codec->extradata_size);
  188. memcpy(st->codec->extradata, s[i].codec_specific, st->codec->extradata_size);
  189. }
  190. avpriv_set_pts_info(avf->streams[i], 60, s[i].time_base.num, s[i].time_base.den);
  191. st->start_time = 0;
  192. st->duration = s[i].max_pts;
  193. st->codec->codec_id = ff_codec_get_id(nut_tags, st->codec->codec_tag);
  194. switch(s[i].type) {
  195. case NUT_AUDIO_CLASS:
  196. st->codec->codec_type = AVMEDIA_TYPE_AUDIO;
  197. if (st->codec->codec_id == CODEC_ID_NONE) st->codec->codec_id = ff_codec_get_id(ff_codec_wav_tags, st->codec->codec_tag);
  198. st->codec->channels = s[i].channel_count;
  199. st->codec->sample_rate = s[i].samplerate_num / s[i].samplerate_denom;
  200. break;
  201. case NUT_VIDEO_CLASS:
  202. st->codec->codec_type = AVMEDIA_TYPE_VIDEO;
  203. if (st->codec->codec_id == CODEC_ID_NONE) st->codec->codec_id = ff_codec_get_id(ff_codec_bmp_tags, st->codec->codec_tag);
  204. st->codec->width = s[i].width;
  205. st->codec->height = s[i].height;
  206. st->sample_aspect_ratio.num = s[i].sample_width;
  207. st->sample_aspect_ratio.den = s[i].sample_height;
  208. break;
  209. }
  210. if (st->codec->codec_id == CODEC_ID_NONE) av_log(avf, AV_LOG_ERROR, "Unknown codec?!\n");
  211. }
  212. return 0;
  213. }
  214. static int nut_read_packet(AVFormatContext * avf, AVPacket * pkt) {
  215. NUTContext * priv = avf->priv_data;
  216. nut_packet_tt pd;
  217. int ret;
  218. ret = nut_read_next_packet(priv->nut, &pd);
  219. if (ret || av_new_packet(pkt, pd.len) < 0) {
  220. if (ret != NUT_ERR_EOF)
  221. av_log(avf, AV_LOG_ERROR, " NUT error: %s\n", nut_error(ret));
  222. return -1;
  223. }
  224. if (pd.flags & NUT_FLAG_KEY) pkt->flags |= AV_PKT_FLAG_KEY;
  225. pkt->pts = pd.pts;
  226. pkt->stream_index = pd.stream;
  227. pkt->pos = avio_tell(avf->pb);
  228. ret = nut_read_frame(priv->nut, &pd.len, pkt->data);
  229. return ret;
  230. }
  231. static int nut_read_seek(AVFormatContext * avf, int stream_index, int64_t target_ts, int flags) {
  232. NUTContext * priv = avf->priv_data;
  233. int active_streams[] = { stream_index, -1 };
  234. double time_pos = target_ts * priv->s[stream_index].time_base.num / (double)priv->s[stream_index].time_base.den;
  235. if (nut_seek(priv->nut, time_pos, 2*!(flags & AVSEEK_FLAG_BACKWARD), active_streams)) return -1;
  236. return 0;
  237. }
  238. static int nut_read_close(AVFormatContext *s) {
  239. NUTContext * priv = s->priv_data;
  240. nut_demuxer_uninit(priv->nut);
  241. return 0;
  242. }
  243. AVInputFormat ff_libnut_demuxer = {
  244. .name = "libnut",
  245. .long_name = NULL_IF_CONFIG_SMALL("NUT format"),
  246. .priv_data_size = sizeof(NUTContext),
  247. .read_probe = nut_probe,
  248. .read_header = nut_read_header,
  249. .read_packet = nut_read_packet,
  250. .read_close = nut_read_close,
  251. .read_seek = nut_read_seek,
  252. .extensions = "nut",
  253. };