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.

284 lines
8.0KB

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