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.

309 lines
9.8KB

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