libnut.c 9.0 KB

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