uncoded_frame.c 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279
  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. typedef struct {
  10. AVFormatContext *mux;
  11. AVStream *stream;
  12. AVFilterContext *sink;
  13. AVFilterLink *link;
  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. st->link = st->sink->inputs[0];
  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. av_register_all();
  61. avdevice_register_all();
  62. avfilter_register_all();
  63. /* Create input graph */
  64. if (!(in_graph = avfilter_graph_alloc())) {
  65. ret = AVERROR(ENOMEM);
  66. av_log(NULL, AV_LOG_ERROR, "Unable to alloc graph graph: %s\n",
  67. av_err2str(ret));
  68. goto fail;
  69. }
  70. ret = avfilter_graph_parse_ptr(in_graph, in_graph_desc, NULL, NULL, NULL);
  71. if (ret < 0) {
  72. av_log(NULL, AV_LOG_ERROR, "Unable to parse graph: %s\n",
  73. av_err2str(ret));
  74. goto fail;
  75. }
  76. nb_streams = 0;
  77. for (i = 0; i < in_graph->nb_filters; i++) {
  78. AVFilterContext *f = in_graph->filters[i];
  79. for (j = 0; j < f->nb_inputs; j++) {
  80. if (!f->inputs[j]) {
  81. av_log(NULL, AV_LOG_ERROR, "Graph has unconnected inputs\n");
  82. ret = AVERROR(EINVAL);
  83. goto fail;
  84. }
  85. }
  86. for (j = 0; j < f->nb_outputs; j++)
  87. if (!f->outputs[j])
  88. nb_streams++;
  89. }
  90. if (!nb_streams) {
  91. av_log(NULL, AV_LOG_ERROR, "Graph has no output stream\n");
  92. ret = AVERROR(EINVAL);
  93. goto fail;
  94. }
  95. if (nb_out_dev != 1 && nb_out_dev != nb_streams) {
  96. av_log(NULL, AV_LOG_ERROR,
  97. "Graph has %d output streams, %d devices given\n",
  98. nb_streams, nb_out_dev);
  99. ret = AVERROR(EINVAL);
  100. goto fail;
  101. }
  102. if (!(streams = av_calloc(nb_streams, sizeof(*streams)))) {
  103. ret = AVERROR(ENOMEM);
  104. av_log(NULL, AV_LOG_ERROR, "Could not allocate streams\n");
  105. }
  106. st = streams;
  107. for (i = 0; i < in_graph->nb_filters; i++) {
  108. AVFilterContext *f = in_graph->filters[i];
  109. for (j = 0; j < f->nb_outputs; j++) {
  110. if (!f->outputs[j]) {
  111. if ((ret = create_sink(st++, in_graph, f, j)) < 0)
  112. goto fail;
  113. }
  114. }
  115. }
  116. av_assert0(st - streams == nb_streams);
  117. if ((ret = avfilter_graph_config(in_graph, NULL)) < 0) {
  118. av_log(NULL, AV_LOG_ERROR, "Failed to configure graph\n");
  119. goto fail;
  120. }
  121. /* Create output devices */
  122. for (i = 0; i < nb_out_dev; i++) {
  123. char *fmt = NULL, *dev = out_dev_name[i];
  124. st = &streams[i];
  125. if ((dev = strchr(dev, ':'))) {
  126. *(dev++) = 0;
  127. fmt = out_dev_name[i];
  128. }
  129. ret = avformat_alloc_output_context2(&st->mux, NULL, fmt, dev);
  130. if (ret < 0) {
  131. av_log(NULL, AV_LOG_ERROR, "Failed to allocate output: %s\n",
  132. av_err2str(ret));
  133. goto fail;
  134. }
  135. if (!(st->mux->oformat->flags & AVFMT_NOFILE)) {
  136. ret = avio_open2(&st->mux->pb, st->mux->filename, AVIO_FLAG_WRITE,
  137. NULL, NULL);
  138. if (ret < 0) {
  139. av_log(st->mux, AV_LOG_ERROR, "Failed to init output: %s\n",
  140. av_err2str(ret));
  141. goto fail;
  142. }
  143. }
  144. }
  145. for (; i < nb_streams; i++)
  146. streams[i].mux = streams[0].mux;
  147. /* Create output device streams */
  148. for (i = 0; i < nb_streams; i++) {
  149. st = &streams[i];
  150. if (!(st->stream = avformat_new_stream(st->mux, NULL))) {
  151. ret = AVERROR(ENOMEM);
  152. av_log(NULL, AV_LOG_ERROR, "Failed to create output stream\n");
  153. goto fail;
  154. }
  155. st->stream->codec->codec_type = st->link->type;
  156. st->stream->time_base = st->stream->codec->time_base =
  157. st->link->time_base;
  158. switch (st->link->type) {
  159. case AVMEDIA_TYPE_VIDEO:
  160. st->stream->codec->codec_id = AV_CODEC_ID_RAWVIDEO;
  161. st->stream->avg_frame_rate =
  162. st->stream-> r_frame_rate = av_buffersink_get_frame_rate(st->sink);
  163. st->stream->codec->width = st->link->w;
  164. st->stream->codec->height = st->link->h;
  165. st->stream->codec->sample_aspect_ratio = st->link->sample_aspect_ratio;
  166. st->stream->codec->pix_fmt = st->link->format;
  167. break;
  168. case AVMEDIA_TYPE_AUDIO:
  169. st->stream->codec->channel_layout = st->link->channel_layout;
  170. st->stream->codec->channels = avfilter_link_get_channels(st->link);
  171. st->stream->codec->sample_rate = st->link->sample_rate;
  172. st->stream->codec->sample_fmt = st->link->format;
  173. st->stream->codec->codec_id =
  174. av_get_pcm_codec(st->stream->codec->sample_fmt, -1);
  175. break;
  176. default:
  177. av_assert0(!"reached");
  178. }
  179. }
  180. /* Init output devices */
  181. for (i = 0; i < nb_out_dev; i++) {
  182. st = &streams[i];
  183. if ((ret = avformat_write_header(st->mux, NULL)) < 0) {
  184. av_log(st->mux, AV_LOG_ERROR, "Failed to init output: %s\n",
  185. av_err2str(ret));
  186. goto fail;
  187. }
  188. }
  189. /* Check output devices */
  190. for (i = 0; i < nb_streams; i++) {
  191. st = &streams[i];
  192. ret = av_write_uncoded_frame_query(st->mux, st->stream->index);
  193. if (ret < 0) {
  194. av_log(st->mux, AV_LOG_ERROR,
  195. "Uncoded frames not supported on stream #%d: %s\n",
  196. i, av_err2str(ret));
  197. goto fail;
  198. }
  199. }
  200. while (run) {
  201. ret = avfilter_graph_request_oldest(in_graph);
  202. if (ret < 0) {
  203. if (ret == AVERROR_EOF) {
  204. run = 0;
  205. } else {
  206. av_log(NULL, AV_LOG_ERROR, "Error filtering: %s\n",
  207. av_err2str(ret));
  208. break;
  209. }
  210. }
  211. for (i = 0; i < nb_streams; i++) {
  212. st = &streams[i];
  213. while (1) {
  214. if (!frame && !(frame = av_frame_alloc())) {
  215. ret = AVERROR(ENOMEM);
  216. av_log(NULL, AV_LOG_ERROR, "Could not allocate frame\n");
  217. goto fail;
  218. }
  219. ret = av_buffersink_get_frame_flags(st->sink, frame,
  220. AV_BUFFERSINK_FLAG_NO_REQUEST);
  221. if (ret < 0) {
  222. if (ret != AVERROR(EAGAIN) && ret != AVERROR_EOF)
  223. av_log(NULL, AV_LOG_WARNING, "Error in sink: %s\n",
  224. av_err2str(ret));
  225. break;
  226. }
  227. if (frame->pts != AV_NOPTS_VALUE)
  228. frame->pts = av_rescale_q(frame->pts,
  229. st->link ->time_base,
  230. st->stream->time_base);
  231. ret = av_interleaved_write_uncoded_frame(st->mux,
  232. st->stream->index,
  233. frame);
  234. frame = NULL;
  235. if (ret < 0) {
  236. av_log(st->stream->codec, AV_LOG_ERROR,
  237. "Error writing frame: %s\n", av_err2str(ret));
  238. goto fail;
  239. }
  240. }
  241. }
  242. }
  243. ret = 0;
  244. for (i = 0; i < nb_out_dev; i++) {
  245. st = &streams[i];
  246. av_write_trailer(st->mux);
  247. }
  248. fail:
  249. av_frame_free(&frame);
  250. avfilter_graph_free(&in_graph);
  251. if (streams) {
  252. for (i = 0; i < nb_out_dev; i++) {
  253. st = &streams[i];
  254. if (st->mux) {
  255. if (st->mux->pb)
  256. avio_closep(&st->mux->pb);
  257. avformat_free_context(st->mux);
  258. }
  259. }
  260. }
  261. av_freep(&streams);
  262. return ret < 0;
  263. }