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.

305 lines
8.9KB

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