uncoded_frame.c 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. #include "libavutil/avassert.h"
  5. #include "libavdevice/avdevice.h"
  6. #include "libavfilter/avfilter.h"
  7. #include "libavfilter/buffersink.h"
  8. #include "libavformat/avformat.h"
  9. #include "libavcodec/codec_id.h"
  10. typedef struct {
  11. AVFormatContext *mux;
  12. AVStream *stream;
  13. AVFilterContext *sink;
  14. } Stream;
  15. static int create_sink(Stream *st, AVFilterGraph *graph,
  16. AVFilterContext *f, int idx)
  17. {
  18. enum AVMediaType type = avfilter_pad_get_type(f->output_pads, idx);
  19. const char *sink_name;
  20. int ret;
  21. switch (type) {
  22. case AVMEDIA_TYPE_VIDEO: sink_name = "buffersink"; break;
  23. case AVMEDIA_TYPE_AUDIO: sink_name = "abuffersink"; break;
  24. default:
  25. av_log(NULL, AV_LOG_ERROR, "Stream type not supported\n");
  26. return AVERROR(EINVAL);
  27. }
  28. ret = avfilter_graph_create_filter(&st->sink,
  29. avfilter_get_by_name(sink_name),
  30. NULL, NULL, NULL, graph);
  31. if (ret < 0)
  32. return ret;
  33. ret = avfilter_link(f, idx, st->sink, 0);
  34. if (ret < 0)
  35. return ret;
  36. return 0;
  37. }
  38. int main(int argc, char **argv)
  39. {
  40. char *in_graph_desc, **out_dev_name;
  41. int nb_out_dev = 0, nb_streams = 0;
  42. AVFilterGraph *in_graph = NULL;
  43. Stream *streams = NULL, *st;
  44. AVFrame *frame = NULL;
  45. int i, j, run = 1, ret;
  46. //av_log_set_level(AV_LOG_DEBUG);
  47. if (argc < 3) {
  48. av_log(NULL, AV_LOG_ERROR,
  49. "Usage: %s filter_graph dev:out [dev2:out2...]\n\n"
  50. "Examples:\n"
  51. "%s movie=file.nut:s=v+a xv:- alsa:default\n"
  52. "%s movie=file.nut:s=v+a uncodedframecrc:pipe:0\n",
  53. argv[0], argv[0], argv[0]);
  54. exit(1);
  55. }
  56. in_graph_desc = argv[1];
  57. out_dev_name = argv + 2;
  58. nb_out_dev = argc - 2;
  59. avdevice_register_all();
  60. /* Create input graph */
  61. if (!(in_graph = avfilter_graph_alloc())) {
  62. ret = AVERROR(ENOMEM);
  63. av_log(NULL, AV_LOG_ERROR, "Unable to alloc graph graph: %s\n",
  64. av_err2str(ret));
  65. goto fail;
  66. }
  67. ret = avfilter_graph_parse_ptr(in_graph, in_graph_desc, NULL, NULL, NULL);
  68. if (ret < 0) {
  69. av_log(NULL, AV_LOG_ERROR, "Unable to parse graph: %s\n",
  70. av_err2str(ret));
  71. goto fail;
  72. }
  73. nb_streams = 0;
  74. for (i = 0; i < in_graph->nb_filters; i++) {
  75. AVFilterContext *f = in_graph->filters[i];
  76. for (j = 0; j < f->nb_inputs; j++) {
  77. if (!f->inputs[j]) {
  78. av_log(NULL, AV_LOG_ERROR, "Graph has unconnected inputs\n");
  79. ret = AVERROR(EINVAL);
  80. goto fail;
  81. }
  82. }
  83. for (j = 0; j < f->nb_outputs; j++)
  84. if (!f->outputs[j])
  85. nb_streams++;
  86. }
  87. if (!nb_streams) {
  88. av_log(NULL, AV_LOG_ERROR, "Graph has no output stream\n");
  89. ret = AVERROR(EINVAL);
  90. goto fail;
  91. }
  92. if (nb_out_dev != 1 && nb_out_dev != nb_streams) {
  93. av_log(NULL, AV_LOG_ERROR,
  94. "Graph has %d output streams, %d devices given\n",
  95. nb_streams, nb_out_dev);
  96. ret = AVERROR(EINVAL);
  97. goto fail;
  98. }
  99. if (!(streams = av_calloc(nb_streams, sizeof(*streams)))) {
  100. ret = AVERROR(ENOMEM);
  101. av_log(NULL, AV_LOG_ERROR, "Could not allocate streams\n");
  102. }
  103. st = streams;
  104. for (i = 0; i < in_graph->nb_filters; i++) {
  105. AVFilterContext *f = in_graph->filters[i];
  106. for (j = 0; j < f->nb_outputs; j++) {
  107. if (!f->outputs[j]) {
  108. if ((ret = create_sink(st++, in_graph, f, j)) < 0)
  109. goto fail;
  110. }
  111. }
  112. }
  113. av_assert0(st - streams == nb_streams);
  114. if ((ret = avfilter_graph_config(in_graph, NULL)) < 0) {
  115. av_log(NULL, AV_LOG_ERROR, "Failed to configure graph\n");
  116. goto fail;
  117. }
  118. /* Create output devices */
  119. for (i = 0; i < nb_out_dev; i++) {
  120. char *fmt = NULL, *dev = out_dev_name[i];
  121. st = &streams[i];
  122. if ((dev = strchr(dev, ':'))) {
  123. *(dev++) = 0;
  124. fmt = out_dev_name[i];
  125. }
  126. ret = avformat_alloc_output_context2(&st->mux, NULL, fmt, dev);
  127. if (ret < 0) {
  128. av_log(NULL, AV_LOG_ERROR, "Failed to allocate output: %s\n",
  129. av_err2str(ret));
  130. goto fail;
  131. }
  132. if (!(st->mux->oformat->flags & AVFMT_NOFILE)) {
  133. ret = avio_open2(&st->mux->pb, st->mux->url, AVIO_FLAG_WRITE,
  134. NULL, NULL);
  135. if (ret < 0) {
  136. av_log(st->mux, AV_LOG_ERROR, "Failed to init output: %s\n",
  137. av_err2str(ret));
  138. goto fail;
  139. }
  140. }
  141. }
  142. for (; i < nb_streams; i++)
  143. streams[i].mux = streams[0].mux;
  144. /* Create output device streams */
  145. for (i = 0; i < nb_streams; i++) {
  146. st = &streams[i];
  147. if (!(st->stream = avformat_new_stream(st->mux, NULL))) {
  148. ret = AVERROR(ENOMEM);
  149. av_log(NULL, AV_LOG_ERROR, "Failed to create output stream\n");
  150. goto fail;
  151. }
  152. st->stream->codecpar->codec_type = av_buffersink_get_type(st->sink);
  153. st->stream->time_base = av_buffersink_get_time_base(st->sink);
  154. switch (av_buffersink_get_type(st->sink)) {
  155. case AVMEDIA_TYPE_VIDEO:
  156. st->stream->codecpar->codec_id = AV_CODEC_ID_RAWVIDEO;
  157. st->stream->avg_frame_rate =
  158. st->stream-> r_frame_rate = av_buffersink_get_frame_rate(st->sink);
  159. st->stream->codecpar->width = av_buffersink_get_w(st->sink);
  160. st->stream->codecpar->height = av_buffersink_get_h(st->sink);
  161. st->stream->codecpar->sample_aspect_ratio = av_buffersink_get_sample_aspect_ratio(st->sink);
  162. st->stream->codecpar->format = av_buffersink_get_format(st->sink);
  163. break;
  164. case AVMEDIA_TYPE_AUDIO:
  165. ret = av_buffersink_get_ch_layout(st->sink, &st->stream->codecpar->ch_layout);
  166. if (ret < 0)
  167. goto fail;
  168. st->stream->codecpar->sample_rate = av_buffersink_get_sample_rate(st->sink);
  169. st->stream->codecpar->format = av_buffersink_get_format(st->sink);
  170. st->stream->codecpar->codec_id = av_get_pcm_codec(st->stream->codecpar->format, -1);
  171. break;
  172. default:
  173. av_assert0(!"reached");
  174. }
  175. }
  176. /* Init output devices */
  177. for (i = 0; i < nb_out_dev; i++) {
  178. st = &streams[i];
  179. if ((ret = avformat_write_header(st->mux, NULL)) < 0) {
  180. av_log(st->mux, AV_LOG_ERROR, "Failed to init output: %s\n",
  181. av_err2str(ret));
  182. goto fail;
  183. }
  184. }
  185. /* Check output devices */
  186. for (i = 0; i < nb_streams; i++) {
  187. st = &streams[i];
  188. ret = av_write_uncoded_frame_query(st->mux, st->stream->index);
  189. if (ret < 0) {
  190. av_log(st->mux, AV_LOG_ERROR,
  191. "Uncoded frames not supported on stream #%d: %s\n",
  192. i, av_err2str(ret));
  193. goto fail;
  194. }
  195. }
  196. while (run) {
  197. ret = avfilter_graph_request_oldest(in_graph);
  198. if (ret < 0) {
  199. if (ret == AVERROR_EOF) {
  200. run = 0;
  201. } else {
  202. av_log(NULL, AV_LOG_ERROR, "Error filtering: %s\n",
  203. av_err2str(ret));
  204. break;
  205. }
  206. }
  207. for (i = 0; i < nb_streams; i++) {
  208. st = &streams[i];
  209. while (1) {
  210. if (!frame && !(frame = av_frame_alloc())) {
  211. ret = AVERROR(ENOMEM);
  212. av_log(NULL, AV_LOG_ERROR, "Could not allocate frame\n");
  213. goto fail;
  214. }
  215. ret = av_buffersink_get_frame_flags(st->sink, frame,
  216. AV_BUFFERSINK_FLAG_NO_REQUEST);
  217. if (ret < 0) {
  218. if (ret != AVERROR(EAGAIN) && ret != AVERROR_EOF)
  219. av_log(NULL, AV_LOG_WARNING, "Error in sink: %s\n",
  220. av_err2str(ret));
  221. break;
  222. }
  223. if (frame->pts != AV_NOPTS_VALUE)
  224. frame->pts = av_rescale_q(frame->pts,
  225. av_buffersink_get_time_base(st->sink),
  226. st->stream->time_base);
  227. ret = av_interleaved_write_uncoded_frame(st->mux,
  228. st->stream->index,
  229. frame);
  230. frame = NULL;
  231. if (ret < 0) {
  232. av_log(st->mux, AV_LOG_ERROR,
  233. "Error writing frame: %s\n", av_err2str(ret));
  234. goto fail;
  235. }
  236. }
  237. }
  238. }
  239. ret = 0;
  240. for (i = 0; i < nb_out_dev; i++) {
  241. st = &streams[i];
  242. av_write_trailer(st->mux);
  243. }
  244. fail:
  245. av_frame_free(&frame);
  246. avfilter_graph_free(&in_graph);
  247. if (streams) {
  248. for (i = 0; i < nb_out_dev; i++) {
  249. st = &streams[i];
  250. if (st->mux) {
  251. if (st->mux->pb)
  252. avio_closep(&st->mux->pb);
  253. avformat_free_context(st->mux);
  254. }
  255. }
  256. }
  257. av_freep(&streams);
  258. return ret < 0;
  259. }