PageRenderTime 58ms CodeModel.GetById 28ms RepoModel.GetById 0ms app.codeStats 0ms

/src/libavformat/libnut.c

https://github.com/AnthonyNystrom/MobiVU
C | 359 lines | 275 code | 58 blank | 26 comment | 37 complexity | 52c69a671113360bf8eeb7bba7343784 MD5 | raw file
  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 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_t * nut;
  33. nut_stream_header_t * 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. #ifdef CONFIG_MUXERS
  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. #ifdef __CW32__
  52. nut_muxer_opts_t mopts;
  53. #else
  54. nut_muxer_opts_t mopts = {
  55. .output = {
  56. .priv = bc,
  57. .write = av_write,
  58. },
  59. .alloc = { av_malloc, av_realloc, av_free },
  60. .write_index = 1,
  61. .realtime_stream = 0,
  62. .max_distance = 32768,
  63. .fti = NULL,
  64. };
  65. #endif
  66. nut_stream_header_t * s;
  67. int i;
  68. #ifdef __CW32__
  69. mopts.output.priv = bc;
  70. mopts.output.write = av_write;
  71. mopts.alloc.malloc = av_malloc;
  72. mopts.alloc.realloc = av_realloc;
  73. mopts.alloc.free = av_free;
  74. mopts.write_index = 1;
  75. mopts.realtime_stream = 0;
  76. mopts.max_distance = 32768;
  77. mopts.fti = NULL;
  78. #endif
  79. priv->s = s = av_mallocz((avf->nb_streams + 1) * sizeof*s);
  80. for (i = 0; i < avf->nb_streams; i++) {
  81. AVCodecContext * codec = avf->streams[i]->codec;
  82. int j;
  83. int fourcc = 0;
  84. int num, denom, ssize;
  85. s[i].type = codec->codec_type == CODEC_TYPE_VIDEO ? NUT_VIDEO_CLASS : NUT_AUDIO_CLASS;
  86. if (codec->codec_tag) fourcc = codec->codec_tag;
  87. else fourcc = codec_get_tag(nut_tags, codec->codec_id);
  88. if (!fourcc) {
  89. if (codec->codec_type == CODEC_TYPE_VIDEO) fourcc = codec_get_tag(codec_bmp_tags, codec->codec_id);
  90. if (codec->codec_type == CODEC_TYPE_AUDIO) fourcc = codec_get_tag(codec_wav_tags, codec->codec_id);
  91. }
  92. s[i].fourcc_len = 4;
  93. s[i].fourcc = av_malloc(s[i].fourcc_len);
  94. for (j = 0; j < s[i].fourcc_len; j++) s[i].fourcc[j] = (fourcc >> (j*8)) & 0xFF;
  95. ff_parse_specific_params(codec, &num, &ssize, &denom);
  96. av_set_pts_info(avf->streams[i], 60, denom, num);
  97. s[i].time_base.num = denom;
  98. s[i].time_base.den = num;
  99. s[i].fixed_fps = 0;
  100. s[i].decode_delay = codec->has_b_frames;
  101. s[i].codec_specific_len = codec->extradata_size;
  102. s[i].codec_specific = codec->extradata;
  103. if (codec->codec_type == CODEC_TYPE_VIDEO) {
  104. s[i].width = codec->width;
  105. s[i].height = codec->height;
  106. s[i].sample_width = 0;
  107. s[i].sample_height = 0;
  108. s[i].colorspace_type = 0;
  109. } else {
  110. s[i].samplerate_num = codec->sample_rate;
  111. s[i].samplerate_denom = 1;
  112. s[i].channel_count = codec->channels;
  113. }
  114. }
  115. s[avf->nb_streams].type = -1;
  116. priv->nut = nut_muxer_init(&mopts, s, NULL);
  117. return 0;
  118. }
  119. static int nut_write_packet(AVFormatContext * avf, AVPacket * pkt) {
  120. NUTContext * priv = avf->priv_data;
  121. nut_packet_t p;
  122. p.len = pkt->size;
  123. p.stream = pkt->stream_index;
  124. p.pts = pkt->pts;
  125. p.flags = pkt->flags & PKT_FLAG_KEY ? NUT_FLAG_KEY : 0;
  126. p.next_pts = 0;
  127. nut_write_frame_reorder(priv->nut, &p, pkt->data);
  128. return 0;
  129. }
  130. static int nut_write_trailer(AVFormatContext * avf) {
  131. ByteIOContext * bc = avf->pb;
  132. NUTContext * priv = avf->priv_data;
  133. int i;
  134. nut_muxer_uninit_reorder(priv->nut);
  135. put_flush_packet(bc);
  136. for(i = 0; priv->s[i].type != -1; i++ ) av_freep(&priv->s[i].fourcc);
  137. av_freep(&priv->s);
  138. return 0;
  139. }
  140. AVOutputFormat libnut_muxer = {
  141. "libnut",
  142. "nut format",
  143. "video/x-nut",
  144. "nut",
  145. sizeof(NUTContext),
  146. CODEC_ID_VORBIS,
  147. CODEC_ID_MPEG4,
  148. nut_write_header,
  149. nut_write_packet,
  150. nut_write_trailer,
  151. #ifdef __CW32__
  152. AVFMT_GLOBALHEADER,
  153. #else
  154. .flags = AVFMT_GLOBALHEADER,
  155. #endif
  156. };
  157. #endif //CONFIG_MUXERS
  158. static int nut_probe(AVProbeData *p) {
  159. if (!memcmp(p->buf, ID_STRING, ID_LENGTH)) return AVPROBE_SCORE_MAX;
  160. return 0;
  161. }
  162. static size_t av_read(void * h, size_t len, uint8_t * buf) {
  163. ByteIOContext * bc = h;
  164. return get_buffer(bc, buf, len);
  165. }
  166. static off_t av_seek(void * h, long long pos, int whence) {
  167. ByteIOContext * bc = h;
  168. if (whence == SEEK_END) {
  169. pos = url_fsize(bc) + pos;
  170. whence = SEEK_SET;
  171. }
  172. return url_fseek(bc, pos, whence);
  173. }
  174. static int nut_read_header(AVFormatContext * avf, AVFormatParameters * ap) {
  175. NUTContext * priv = avf->priv_data;
  176. ByteIOContext * bc = avf->pb;
  177. #ifdef __CW32__
  178. nut_demuxer_opts_t dopts;
  179. #else
  180. nut_demuxer_opts_t dopts = {
  181. .input = {
  182. .priv = bc,
  183. .seek = av_seek,
  184. .read = av_read,
  185. .eof = NULL,
  186. .file_pos = 0,
  187. },
  188. .alloc = { av_malloc, av_realloc, av_free },
  189. .read_index = 1,
  190. .cache_syncpoints = 1,
  191. };
  192. #endif
  193. nut_context_t * nut = priv->nut = nut_demuxer_init(&dopts);
  194. nut_stream_header_t * s;
  195. int ret, i;
  196. #ifdef __CW32__
  197. dopts.input.priv = bc;
  198. dopts.input.seek = av_seek;
  199. dopts.input.read = av_read;
  200. dopts.input.eof = NULL;
  201. dopts.input.file_pos = 0;
  202. dopts.alloc.malloc = av_malloc;
  203. dopts.alloc.realloc = av_realloc;
  204. dopts.alloc.free = av_free;
  205. dopts.read_index = 1;
  206. dopts.cache_syncpoints = 1;
  207. #endif
  208. if ((ret = nut_read_headers(nut, &s, NULL))) {
  209. av_log(avf, AV_LOG_ERROR, " NUT error: %s\n", nut_error(ret));
  210. nut_demuxer_uninit(nut);
  211. return -1;
  212. }
  213. priv->s = s;
  214. for (i = 0; s[i].type != -1 && i < 2; i++) {
  215. AVStream * st = av_new_stream(avf, i);
  216. int j;
  217. for (j = 0; j < s[i].fourcc_len && j < 8; j++) st->codec->codec_tag |= s[i].fourcc[j]<<(j*8);
  218. st->codec->has_b_frames = s[i].decode_delay;
  219. st->codec->extradata_size = s[i].codec_specific_len;
  220. if (st->codec->extradata_size) {
  221. st->codec->extradata = av_mallocz(st->codec->extradata_size);
  222. memcpy(st->codec->extradata, s[i].codec_specific, st->codec->extradata_size);
  223. }
  224. av_set_pts_info(avf->streams[i], 60, s[i].time_base.num, s[i].time_base.den);
  225. st->start_time = 0;
  226. st->duration = s[i].max_pts;
  227. st->codec->codec_id = codec_get_id(nut_tags, st->codec->codec_tag);
  228. switch(s[i].type) {
  229. case NUT_AUDIO_CLASS:
  230. st->codec->codec_type = CODEC_TYPE_AUDIO;
  231. if (st->codec->codec_id == CODEC_ID_NONE) st->codec->codec_id = codec_get_id(codec_wav_tags, st->codec->codec_tag);
  232. st->codec->channels = s[i].channel_count;
  233. st->codec->sample_rate = s[i].samplerate_num / s[i].samplerate_denom;
  234. break;
  235. case NUT_VIDEO_CLASS:
  236. st->codec->codec_type = CODEC_TYPE_VIDEO;
  237. if (st->codec->codec_id == CODEC_ID_NONE) st->codec->codec_id = codec_get_id(codec_bmp_tags, st->codec->codec_tag);
  238. st->codec->width = s[i].width;
  239. st->codec->height = s[i].height;
  240. st->codec->sample_aspect_ratio.num = s[i].sample_width;
  241. st->codec->sample_aspect_ratio.den = s[i].sample_height;
  242. break;
  243. }
  244. if (st->codec->codec_id == CODEC_ID_NONE) av_log(avf, AV_LOG_ERROR, "Unknown codec?!\n");
  245. }
  246. return 0;
  247. }
  248. static int nut_read_packet(AVFormatContext * avf, AVPacket * pkt) {
  249. NUTContext * priv = avf->priv_data;
  250. nut_packet_t pd;
  251. int ret;
  252. ret = nut_read_next_packet(priv->nut, &pd);
  253. if (ret || av_new_packet(pkt, pd.len) < 0) {
  254. if (ret != NUT_ERR_EOF)
  255. av_log(avf, AV_LOG_ERROR, " NUT error: %s\n", nut_error(ret));
  256. return -1;
  257. }
  258. if (pd.flags & NUT_FLAG_KEY) pkt->flags |= PKT_FLAG_KEY;
  259. pkt->pts = pd.pts;
  260. pkt->stream_index = pd.stream;
  261. pkt->pos = url_ftell(avf->pb);
  262. ret = nut_read_frame(priv->nut, &pd.len, pkt->data);
  263. return ret;
  264. }
  265. static int nut_read_seek(AVFormatContext * avf, int stream_index, int64_t target_ts, int flags) {
  266. NUTContext * priv = avf->priv_data;
  267. #ifdef __CW32__
  268. int active_streams[2];
  269. #else
  270. int active_streams[] = { stream_index, -1 };
  271. #endif
  272. double time_pos = target_ts * priv->s[stream_index].time_base.num / (double)priv->s[stream_index].time_base.den;
  273. #ifdef __CW32__
  274. active_streams[0] = stream_index;
  275. active_streams[1] = -1;
  276. #endif
  277. if (nut_seek(priv->nut, time_pos, 2*!(flags & AVSEEK_FLAG_BACKWARD), active_streams)) return -1;
  278. return 0;
  279. }
  280. static int nut_read_close(AVFormatContext *s) {
  281. NUTContext * priv = s->priv_data;
  282. nut_demuxer_uninit(priv->nut);
  283. return 0;
  284. }
  285. AVInputFormat libnut_demuxer = {
  286. "libnut",
  287. NULL_IF_CONFIG_SMALL("NUT format"),
  288. sizeof(NUTContext),
  289. nut_probe,
  290. nut_read_header,
  291. nut_read_packet,
  292. nut_read_close,
  293. nut_read_seek,
  294. #ifdef __CW32__
  295. 0,
  296. 0,
  297. "nut",
  298. #else
  299. .extensions = "nut",
  300. #endif
  301. };