libnut.c 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322
  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
  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. if(!s)
  67. return AVERROR(ENOMEM);
  68. for (i = 0; i < avf->nb_streams; i++) {
  69. AVCodecContext * codec = avf->streams[i]->codec;
  70. int j;
  71. int fourcc = 0;
  72. int num, denom, ssize;
  73. s[i].type = codec->codec_type == AVMEDIA_TYPE_VIDEO ? NUT_VIDEO_CLASS : NUT_AUDIO_CLASS;
  74. if (codec->codec_tag) fourcc = codec->codec_tag;
  75. else fourcc = ff_codec_get_tag(nut_tags, codec->codec_id);
  76. if (!fourcc) {
  77. if (codec->codec_type == AVMEDIA_TYPE_VIDEO) fourcc = ff_codec_get_tag(ff_codec_bmp_tags, codec->codec_id);
  78. if (codec->codec_type == AVMEDIA_TYPE_AUDIO) fourcc = ff_codec_get_tag(ff_codec_wav_tags, codec->codec_id);
  79. }
  80. s[i].fourcc_len = 4;
  81. s[i].fourcc = av_malloc(s[i].fourcc_len);
  82. for (j = 0; j < s[i].fourcc_len; j++) s[i].fourcc[j] = (fourcc >> (j*8)) & 0xFF;
  83. ff_parse_specific_params(codec, &num, &ssize, &denom);
  84. avpriv_set_pts_info(avf->streams[i], 60, denom, num);
  85. s[i].time_base.num = denom;
  86. s[i].time_base.den = num;
  87. s[i].fixed_fps = 0;
  88. s[i].decode_delay = codec->has_b_frames;
  89. s[i].codec_specific_len = codec->extradata_size;
  90. s[i].codec_specific = codec->extradata;
  91. if (codec->codec_type == AVMEDIA_TYPE_VIDEO) {
  92. s[i].width = codec->width;
  93. s[i].height = codec->height;
  94. s[i].sample_width = 0;
  95. s[i].sample_height = 0;
  96. s[i].colorspace_type = 0;
  97. } else {
  98. s[i].samplerate_num = codec->sample_rate;
  99. s[i].samplerate_denom = 1;
  100. s[i].channel_count = codec->channels;
  101. }
  102. }
  103. s[avf->nb_streams].type = -1;
  104. priv->nut = nut_muxer_init(&mopts, s, NULL);
  105. return 0;
  106. }
  107. static int nut_write_packet(AVFormatContext * avf, AVPacket * pkt) {
  108. NUTContext * priv = avf->priv_data;
  109. nut_packet_tt p;
  110. p.len = pkt->size;
  111. p.stream = pkt->stream_index;
  112. p.pts = pkt->pts;
  113. p.flags = pkt->flags & AV_PKT_FLAG_KEY ? NUT_FLAG_KEY : 0;
  114. p.next_pts = 0;
  115. nut_write_frame_reorder(priv->nut, &p, pkt->data);
  116. return 0;
  117. }
  118. static int nut_write_trailer(AVFormatContext * avf) {
  119. AVIOContext * bc = avf->pb;
  120. NUTContext * priv = avf->priv_data;
  121. int i;
  122. nut_muxer_uninit_reorder(priv->nut);
  123. avio_flush(bc);
  124. for(i = 0; priv->s[i].type != -1; i++ ) av_freep(&priv->s[i].fourcc);
  125. av_freep(&priv->s);
  126. return 0;
  127. }
  128. AVOutputFormat ff_libnut_muxer = {
  129. .name = "libnut",
  130. .long_name = "nut format",
  131. .mime_type = "video/x-nut",
  132. .extensions = "nut",
  133. .priv_data_size = sizeof(NUTContext),
  134. .audio_codec = CODEC_ID_VORBIS,
  135. .video_codec = CODEC_ID_MPEG4,
  136. .write_header = nut_write_header,
  137. .write_packet = nut_write_packet,
  138. .write_trailer = nut_write_trailer,
  139. .flags = AVFMT_GLOBALHEADER,
  140. };
  141. #endif /* CONFIG_LIBNUT_MUXER */
  142. static int nut_probe(AVProbeData *p) {
  143. if (!memcmp(p->buf, ID_STRING, ID_LENGTH)) return AVPROBE_SCORE_MAX;
  144. return 0;
  145. }
  146. static size_t av_read(void * h, size_t len, uint8_t * buf) {
  147. AVIOContext * bc = h;
  148. return avio_read(bc, buf, len);
  149. }
  150. static off_t av_seek(void * h, long long pos, int whence) {
  151. AVIOContext * bc = h;
  152. if (whence == SEEK_END) {
  153. pos = avio_size(bc) + pos;
  154. whence = SEEK_SET;
  155. }
  156. return avio_seek(bc, pos, whence);
  157. }
  158. static int nut_read_header(AVFormatContext * avf, AVFormatParameters * ap) {
  159. NUTContext * priv = avf->priv_data;
  160. AVIOContext * bc = avf->pb;
  161. nut_demuxer_opts_tt dopts = {
  162. .input = {
  163. .priv = bc,
  164. .seek = av_seek,
  165. .read = av_read,
  166. .eof = NULL,
  167. .file_pos = 0,
  168. },
  169. .alloc = { av_malloc, av_realloc, av_free },
  170. .read_index = 1,
  171. .cache_syncpoints = 1,
  172. };
  173. nut_context_tt * nut = priv->nut = nut_demuxer_init(&dopts);
  174. nut_stream_header_tt * s;
  175. int ret, i;
  176. if(!nut)
  177. return -1;
  178. if ((ret = nut_read_headers(nut, &s, NULL))) {
  179. av_log(avf, AV_LOG_ERROR, " NUT error: %s\n", nut_error(ret));
  180. nut_demuxer_uninit(nut);
  181. priv->nut = NULL;
  182. return -1;
  183. }
  184. priv->s = s;
  185. for (i = 0; s[i].type != -1 && i < 2; i++) {
  186. AVStream * st = avformat_new_stream(avf, NULL);
  187. int j;
  188. for (j = 0; j < s[i].fourcc_len && j < 8; j++) st->codec->codec_tag |= s[i].fourcc[j]<<(j*8);
  189. st->codec->has_b_frames = s[i].decode_delay;
  190. st->codec->extradata_size = s[i].codec_specific_len;
  191. if (st->codec->extradata_size) {
  192. st->codec->extradata = av_mallocz(st->codec->extradata_size);
  193. if(!st->codec->extradata){
  194. nut_demuxer_uninit(nut);
  195. priv->nut = NULL;
  196. return AVERROR(ENOMEM);
  197. }
  198. memcpy(st->codec->extradata, s[i].codec_specific, st->codec->extradata_size);
  199. }
  200. avpriv_set_pts_info(avf->streams[i], 60, s[i].time_base.num, s[i].time_base.den);
  201. st->start_time = 0;
  202. st->duration = s[i].max_pts;
  203. st->codec->codec_id = ff_codec_get_id(nut_tags, st->codec->codec_tag);
  204. switch(s[i].type) {
  205. case NUT_AUDIO_CLASS:
  206. st->codec->codec_type = AVMEDIA_TYPE_AUDIO;
  207. if (st->codec->codec_id == CODEC_ID_NONE) st->codec->codec_id = ff_codec_get_id(ff_codec_wav_tags, st->codec->codec_tag);
  208. st->codec->channels = s[i].channel_count;
  209. st->codec->sample_rate = s[i].samplerate_num / s[i].samplerate_denom;
  210. break;
  211. case NUT_VIDEO_CLASS:
  212. st->codec->codec_type = AVMEDIA_TYPE_VIDEO;
  213. if (st->codec->codec_id == CODEC_ID_NONE) st->codec->codec_id = ff_codec_get_id(ff_codec_bmp_tags, st->codec->codec_tag);
  214. st->codec->width = s[i].width;
  215. st->codec->height = s[i].height;
  216. st->sample_aspect_ratio.num = s[i].sample_width;
  217. st->sample_aspect_ratio.den = s[i].sample_height;
  218. break;
  219. }
  220. if (st->codec->codec_id == CODEC_ID_NONE) av_log(avf, AV_LOG_ERROR, "Unknown codec?!\n");
  221. }
  222. return 0;
  223. }
  224. static int nut_read_packet(AVFormatContext * avf, AVPacket * pkt) {
  225. NUTContext * priv = avf->priv_data;
  226. nut_packet_tt pd;
  227. int ret;
  228. ret = nut_read_next_packet(priv->nut, &pd);
  229. if (ret || av_new_packet(pkt, pd.len) < 0) {
  230. if (ret != NUT_ERR_EOF)
  231. av_log(avf, AV_LOG_ERROR, " NUT error: %s\n", nut_error(ret));
  232. return -1;
  233. }
  234. if (pd.flags & NUT_FLAG_KEY) pkt->flags |= AV_PKT_FLAG_KEY;
  235. pkt->pts = pd.pts;
  236. pkt->stream_index = pd.stream;
  237. pkt->pos = avio_tell(avf->pb);
  238. ret = nut_read_frame(priv->nut, &pd.len, pkt->data);
  239. return ret;
  240. }
  241. static int nut_read_seek(AVFormatContext * avf, int stream_index, int64_t target_ts, int flags) {
  242. NUTContext * priv = avf->priv_data;
  243. int active_streams[] = { stream_index, -1 };
  244. double time_pos = target_ts * priv->s[stream_index].time_base.num / (double)priv->s[stream_index].time_base.den;
  245. if (nut_seek(priv->nut, time_pos, 2*!(flags & AVSEEK_FLAG_BACKWARD), active_streams)) return -1;
  246. return 0;
  247. }
  248. static int nut_read_close(AVFormatContext *s) {
  249. NUTContext * priv = s->priv_data;
  250. nut_demuxer_uninit(priv->nut);
  251. return 0;
  252. }
  253. AVInputFormat ff_libnut_demuxer = {
  254. .name = "libnut",
  255. .long_name = NULL_IF_CONFIG_SMALL("NUT format"),
  256. .priv_data_size = sizeof(NUTContext),
  257. .read_probe = nut_probe,
  258. .read_header = nut_read_header,
  259. .read_packet = nut_read_packet,
  260. .read_close = nut_read_close,
  261. .read_seek = nut_read_seek,
  262. .extensions = "nut",
  263. };