Audio and video streams: ffplay: tutorial01

Source: Internet
Author: User

Original:

Http://blog.chinaunix.net/uid-24203478-id-3035890.html

 

The version used is FFMPEG 0.6.3.

Use the compilation command:
Gcc-O tutorial01 tutorial01.c-lavutil-lavformat-lavcodec-lswscale

The source program is as follows:

[CPP]
View plaincopy
  1. # Include <libavcodec/avcodec. h>
  2. # Include <libavformat/avformat. h>
  3. # Include <libswscale/swscale. h>
  4. # Include <stdio. h>
  5. # Define debug () fprintf (stderr, "% s # % I \ n" ,__ func __,__ line __)
  6. Static struct swscontext * img_convert_ctx;
  7. Void saveframe (avframe * pframe, int width, int height, int IFRAME)
  8. {
  9. File * pfile;
  10. Char szfilename [32];
  11. Int y;
  12. Sprintf (szfilename, "frame % d. ppm", IFRAME );
  13. Pfile = fopen (szfilename, "WB ");
  14. If (pfile = NULL ){
  15. Return;
  16. }
  17. Fprintf (pfile, "P6 \ n % d \ n255 \ n", width, height );
  18. For (y = 0; y
  19. Fwrite (pframe-> data [0] + y * pframe-> linesize [0], 1, width * 3, pfile );
  20. }
  21. Fclose (pfile );
  22. }
  23. Int main (INT argc, char * argv [])
  24. {
  25. Avformatcontext * pformatctx;
  26. Int I, videostream;
  27. Avcodeccontext * pcodecctx;
  28. Avcodec * pcodec;
  29. Avframe * pframe;
  30. Avframe * pframergb;
  31. Avpacket packet;
  32. Int framefinished;
  33. Int numbytes;
  34. Uint8_t * buffer;
  35. If (argc <2 ){
  36. Printf ("Please provide a movie file \ n ");
  37. Return-1;
  38. }
  39. Fprintf (stderr, "Arg OK. \ n ");
  40. Debug ();
  41. Av_register_all ();
  42. Fprintf (stderr, "av_register_all () OK. \ n ");
  43. Debug ();
  44. Pformatctx = avformat_alloc_context (); // allocate space
  45. Fprintf (stderr, "avformat_alloc_context () OK. \ n ");
  46. Debug ();
  47. If (av_open_input_file (& pformatctx, argv [1], null, 0, null )! = 0 ){
  48. // If (avformat_open_input (& pformatctx, argv [1], null, null )! = 0) {// open the input file
  49. Return-1;
  50. }
  51. Fprintf (stderr, "avformat_open_input () OK. \ n ");
  52. Debug ();
  53. If (av_find_stream_info (pformatctx) <0 ){
  54. Return-1;
  55. }
  56. Fprintf (stderr, "av_find_stream_info () OK. \ n ");
  57. Debug ();
  58. Dump_format (pformatctx, 0, argv [1], 0 );
  59. Fprintf (stderr, "dump_format () OK. \ n ");
  60. Debug ();
  61. Videostream =-1;
  62. For (I = 0; I <pformatctx-> nb_streams; I ++ ){
  63. If (pformatctx-> streams [I]-> codec-> codec_type = codec_type_video ){
  64. Videostream = I;
  65. Break;
  66. }
  67. }
  68. Fprintf (stderr, "Find codec_type_video OK. \ n ");
  69. Debug ();
  70. If (videostream =-1 ){
  71. Return-1;
  72. }
  73. Debug ();
  74. Pcodecctx = pformatctx-> streams [videostream]-> codec;
  75. Img_convert_ctx = sws_getcontext (
  76. Pcodecctx-> width,
  77. Pcodecctx-> height,
  78. Pcodecctx-> pix_fmt,
  79. Pcodecctx-> width,
  80. Pcodecctx-> height,
  81. Pix_fmt_rgb24,
  82. Sws_bicubic,
  83. Null, null, null );
  84. Fprintf (stderr, "sws_getcontext () OK. \ n ");
  85. Debug ();
  86. Pcodec = avcodec_find_decoder (pcodecctx-> codec_id );
  87. If (pcodec = NULL ){
  88. Fprintf (stderr, "unsupported codec! \ N ");
  89. Return-1;
  90. }
  91. Fprintf (stderr, "avcodec_find_decoder () OK. \ n ");
  92. Debug ();
  93. If (avcodec_open (pcodecctx, pcodec) <0 ){
  94. Return-1;
  95. }
  96. Fprintf (stderr, "avcodec_open () OK. \ n ");
  97. Debug ();
  98. Pframe = avcodec_alloc_frame ();
  99. Fprintf (stderr, "avcodec_alloc_frame () OK. \ n ");
  100. Debug ();
  101. Pframergb = avcodec_alloc_frame ();
  102. If (pframergb = NULL ){
  103. Return-1;
  104. }
  105. Fprintf (stderr, "avcodec_alloc_frame () OK. \ n ");
  106. Debug ();
  107. Numbytes = avpicture_get_size (pix_fmt_rgb24, pcodecctx-> width,
  108. Pcodecctx-> height );
  109. Fprintf (stderr, "avpicture_get_size () OK. \ n ");
  110. Debug ();
  111. Buffer = (uint8_t *) av_malloc (numbytes * sizeof (uint8_t ));
  112. Debug ();
  113. Avpicture_fill (avpicture *) pframergb, buffer, pix_fmt_rgb24,
  114. Pcodecctx-> width, pcodecctx-> height );
  115. Fprintf (stderr, "avpicture_fill () OK. \ n ");
  116. Debug (); I = 0;
  117. While (av_read_frame (pformatctx, & Packet)> = 0 ){
  118. If (packet. stream_index = videostream ){
  119. Avcodec_decode_video (pcodecctx, pframe,
  120. & Framefinished,
  121. Packet. Data,
  122. Packet. size );
  123. If (framefinished ){
  124. Sws_scale (img_convert_ctx,
  125. (Const uint8_t **) pframe-> data,
  126. Pframe-> linesize, 0,
  127. Pcodecctx-> height,
  128. Pframergb-> data,
  129. Pframergb-> linesize );
  130. If (++ I <= 5 ){
  131. Saveframe (pframergb, pcodecctx-> width, pcodecctx-> height, I );
  132. }
  133. }
  134. }
  135. Av_free_packet (& Packet );
  136. }
  137. Debug ();
  138. Av_free (buffer );
  139. Av_free (pframergb );
  140. Av_free (pframe );
  141. Avcodec_close (pcodecctx );
  142. Av_close_input_file (pformatctx );
  143. Return 0;
  144. }

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.