uncoded_frame.c 9.4 KB

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