音视频流::ffplay::tutorial03

来源:互联网 发布:淘宝开店协议怎么查 编辑:程序博客网 时间:2024/05/16 04:50

转自:

http://blog.chinaunix.net/uid-24203478-id-3171848.html

 

所用的版本为ffmpeg 0.6.3

编译totorial03.c的命令为:
gcc -o tutorial03 tutorial03.c -lavformat -lavcodec -lz -lm -lswscale `sdl-config --cflags --libs`

[cpp] view plaincopy
  1. #include <libavcodec/avcodec.h>   
  2.   
  3.     #include <libavformat/avformat.h>   
  4.   
  5.     #include <libswscale/swscale.h>   
  6.   
  7.         
  8.   
  9.     #include <SDL/SDL.h>   
  10.   
  11.     #include <SDL/SDL_thread.h>   
  12.   
  13.         
  14.   
  15.     #include <stdio.h>   
  16.   
  17.         
  18.   
  19.     #define SDL_AUDIO_BUFFER_SIZE 1024   
  20.   
  21.         
  22.   
  23.         
  24.   
  25.     typedef struct PacketQueue {   
  26.   
  27.         AVPacketList *first_pkt, *last_pkt;   
  28.   
  29.         int nb_packets;   
  30.   
  31.         int size;   
  32.   
  33.         SDL_mutex *mutex;   
  34.   
  35.         SDL_cond *cond;   
  36.   
  37.     } PacketQueue;   
  38.   
  39.         
  40.   
  41.     PacketQueue audioq;   
  42.   
  43.         
  44.   
  45.     int quit = 0;   
  46.   
  47.         
  48.   
  49.     void packet_queue_init(PacketQueue *q) {   
  50.   
  51.         memset(q, 0, sizeof(PacketQueue));   
  52.   
  53.         q->mutex = SDL_CreateMutex();   
  54.   
  55.         q->cond = SDL_CreateCond();   
  56.   
  57.     }   
  58.   
  59.         
  60.   
  61.     int packet_queue_put(PacketQueue *q, AVPacket *pkt) {   
  62.   
  63.         AVPacketList *pkt1;   
  64.   
  65.         if (av_dup_packet(pkt) < 0) {   
  66.   
  67.             return -1;   
  68.   
  69.         }   
  70.   
  71.         pkt1 = av_malloc(sizeof(AVPacketList));   
  72.   
  73.         if (!pkt1) {   
  74.   
  75.             return -1;   
  76.   
  77.         }   
  78.   
  79.         pkt1->pkt = *pkt;   
  80.   
  81.         pkt1->next = NULL;   
  82.   
  83.         
  84.   
  85.         SDL_LockMutex(q->mutex);   
  86.   
  87.         
  88.   
  89.         if (!q->last_pkt) {   
  90.   
  91.             q->first_pkt = pkt1;   
  92.   
  93.         } else {   
  94.   
  95.             q->last_pkt->next = pkt1;   
  96.   
  97.         }   
  98.   
  99.         q->last_pkt = pkt1;   
  100.   
  101.         q->nb_packets++;   
  102.   
  103.         q->size += pkt1->pkt.size;   
  104.   
  105.         SDL_CondSignal(q->cond);   
  106.   
  107.         
  108.   
  109.         SDL_UnlockMutex(q->mutex);   
  110.   
  111.         return 0;   
  112.   
  113.     }   
  114.   
  115.         
  116.   
  117.     static int packet_queue_get(PacketQueue *q, AVPacket *pkt, int block)   
  118.   
  119.     {   
  120.   
  121.         AVPacketList *pkt1;   
  122.   
  123.         int ret;   
  124.   
  125.         
  126.   
  127.         SDL_LockMutex(q->mutex);   
  128.   
  129.         
  130.   
  131.         for (;;) {   
  132.   
  133.             if (quit) {   
  134.   
  135.                 ret = -1;   
  136.   
  137.                 break;   
  138.   
  139.             }   
  140.   
  141.         
  142.   
  143.             pkt1 = q->first_pkt;   
  144.   
  145.             if (pkt1) {   
  146.   
  147.                 q->first_pkt = pkt1->next;   
  148.   
  149.                 if (!q->first_pkt) {   
  150.   
  151.                     q->last_pkt = NULL;   
  152.   
  153.                 }   
  154.   
  155.                 q->nb_packets--;   
  156.   
  157.                 q->size -= pkt1->pkt.size;   
  158.   
  159.                 *pkt = pkt1->pkt;   
  160.   
  161.                 av_free(pkt1);   
  162.   
  163.                 ret = 1;   
  164.   
  165.                 break;   
  166.   
  167.             } else if (!block) {   
  168.   
  169.                 ret = 0;   
  170.   
  171.                 break;   
  172.   
  173.             } else {   
  174.   
  175.                 SDL_CondWait(q->cond, q->mutex);   
  176.   
  177.             }   
  178.   
  179.         }   
  180.   
  181.         SDL_UnlockMutex(q->mutex);   
  182.   
  183.         return ret;   
  184.   
  185.     }   
  186.   
  187.         
  188.   
  189.     int audio_decode_frame(AVCodecContext *aCodecCtx, uint8_t *audio_buf, int buf_size)   
  190.   
  191.     {   
  192.   
  193.         static AVPacket pkt;   
  194.   
  195.         static uint8_t *audio_pkt_data = NULL;   
  196.   
  197.         static int audio_pkt_size = 0;   
  198.   
  199.         
  200.   
  201.         int len1, data_size;   
  202.   
  203.         
  204.   
  205.         for (;;) {   
  206.   
  207.             while(audio_pkt_size > 0) {   
  208.   
  209.                 data_size = buf_size;   
  210.   
  211.                 len1 = avcodec_decode_audio2(aCodecCtx, (int16_t*)audio_buf, &data_size, audio_pkt_data, audio_pkt_size);   
  212.   
  213.                 if (len1 < 0) {   
  214.   
  215.                     audio_pkt_size = 0;   
  216.   
  217.                     break;   
  218.   
  219.                 }   
  220.   
  221.                 audio_pkt_data += len1;   
  222.   
  223.                 audio_pkt_size -= len1;   
  224.   
  225.                 if (data_size <= 0) {   
  226.   
  227.                     continue;   
  228.   
  229.                 }   
  230.   
  231.                 return data_size;   
  232.   
  233.             }   
  234.   
  235.             if (pkt.data) {   
  236.   
  237.                 av_free_packet(&pkt);   
  238.   
  239.             }   
  240.   
  241.             if (quit) {   
  242.   
  243.                 return -1;   
  244.   
  245.             }   
  246.   
  247.         
  248.   
  249.             if (packet_queue_get(&audioq, &pkt, 1) < 0) {   
  250.   
  251.                 return -1;   
  252.   
  253.             }   
  254.   
  255.             audio_pkt_data = pkt.data;   
  256.   
  257.             audio_pkt_size = pkt.size;   
  258.   
  259.         }   
  260.   
  261.     }   
  262.   
  263.         
  264.   
  265.     void audio_callback(void *userdata, Uint8 *stream, int len)   
  266.   
  267.     {   
  268.   
  269.         AVCodecContext *aCodecCtx = (AVCodecContext *)userdata;   
  270.   
  271.         int len1, audio_size;   
  272.   
  273.         
  274.   
  275.         static uint8_t audio_buf[(AVCODEC_MAX_AUDIO_FRAME_SIZE * 3) / 2];   
  276.   
  277.         static unsigned int audio_buf_size = 0;   
  278.   
  279.         static unsigned int audio_buf_index = 0;   
  280.   
  281.         
  282.   
  283.         while (len > 0) {   
  284.   
  285.             if (audio_buf_index >= audio_buf_size) {   
  286.   
  287.                 audio_size = audio_decode_frame(aCodecCtx, audio_buf, sizeof(audio_buf));   
  288.   
  289.                 if (audio_size < 0) {   
  290.   
  291.                     audio_buf_size = 1024;   
  292.   
  293.                     memset(audio_buf, 0, audio_buf_size);   
  294.   
  295.                 } else {   
  296.   
  297.                     audio_buf_size = audio_size;   
  298.   
  299.                 }   
  300.   
  301.                 audio_buf_index = 0;   
  302.   
  303.             }   
  304.   
  305.             len1 = audio_buf_size - audio_buf_index;   
  306.   
  307.             if (len1 > len) {   
  308.   
  309.                 len1 = len;   
  310.   
  311.             }   
  312.   
  313.             memcpy(stream, (uint8_t*)audio_buf + audio_buf_index, len1);   
  314.   
  315.             len -= len1;   
  316.   
  317.             stream += len1;   
  318.   
  319.             audio_buf_index += len1;   
  320.   
  321.         }   
  322.   
  323.     }   
  324.   
  325.         
  326.   
  327.     int main(int argc, char *argv[]) {   
  328.   
  329.         AVFormatContext *pFormatCtx;   
  330.   
  331.         int i, videoStream, audioStream;   
  332.   
  333.         AVCodecContext *pCodecCtx;   
  334.   
  335.         AVCodec *pCodec;   
  336.   
  337.         AVFrame *pFrame;   
  338.   
  339.         AVPacket packet;   
  340.   
  341.         int frameFinished;   
  342.   
  343.         float aspect_ratio;   
  344.   
  345.         struct SwsContext *img_convert_ctx;   
  346.   
  347.         
  348.   
  349.         AVCodecContext *aCodecCtx;   
  350.   
  351.         AVCodec *aCodec;   
  352.   
  353.         
  354.   
  355.         SDL_Overlay *bmp;   
  356.   
  357.         SDL_Surface *screen;   
  358.   
  359.         SDL_Rect rect;   
  360.   
  361.         SDL_Event event;   
  362.   
  363.         SDL_AudioSpec wanted_spec, spec;   
  364.   
  365.         
  366.   
  367.         if (argc < 2) {   
  368.   
  369.             fprintf(stderr, "Usage: test <file>\n");   
  370.   
  371.             exit(1);   
  372.   
  373.         }   
  374.   
  375.         av_register_all();   
  376.   
  377.         
  378.   
  379.         if (SDL_Init(SDL_INIT_VIDEO | SDL_INIT_AUDIO | SDL_INIT_TIMER)) {   
  380.   
  381.             fprintf(stderr, "Could not initialize SDL - %s\n", SDL_GetError());   
  382.   
  383.             exit(1);   
  384.   
  385.         }   
  386.   
  387.         
  388.   
  389.         if (av_open_input_file(&pFormatCtx, argv[1], NULL, 0, NULL) != 0) {   
  390.   
  391.             return -1;   
  392.   
  393.         }   
  394.   
  395.         
  396.   
  397.         if (av_find_stream_info(pFormatCtx) < 0) {   
  398.   
  399.             return -1;   
  400.   
  401.         }   
  402.   
  403.         
  404.   
  405.         dump_format(pFormatCtx, 0, argv[1], 0);   
  406.   
  407.         
  408.   
  409.         videoStream = -1;   
  410.   
  411.         audioStream = -1;   
  412.   
  413.         for (i=0; i<pFormatCtx->nb_streams; i++) {   
  414.   
  415.             if (pFormatCtx->streams[i]->codec->codec_type==CODEC_TYPE_VIDEO && videoStream < 0) {   
  416.   
  417.                 videoStream = i;   
  418.   
  419.             }   
  420.   
  421.             if (pFormatCtx->streams[i]->codec->codec_type == CODEC_TYPE_AUDIO && audioStream < 0) {   
  422.   
  423.                 audioStream = i;   
  424.   
  425.             }   
  426.   
  427.         }   
  428.   
  429.         
  430.   
  431.         if (videoStream == -1) {   
  432.   
  433.             return -1;   
  434.   
  435.         }   
  436.   
  437.         if (audioStream == -1) {   
  438.   
  439.             return -1;   
  440.   
  441.         }   
  442.   
  443.         
  444.   
  445.         aCodecCtx = pFormatCtx->streams[audioStream]->codec;   
  446.   
  447.         
  448.   
  449.         wanted_spec.freq = aCodecCtx->sample_rate;   
  450.   
  451.         wanted_spec.format = AUDIO_S16SYS;   
  452.   
  453.         wanted_spec.channels = aCodecCtx->channels;   
  454.   
  455.         wanted_spec.silence = 0;   
  456.   
  457.         wanted_spec.samples = SDL_AUDIO_BUFFER_SIZE;   
  458.   
  459.         wanted_spec.callback = audio_callback;   
  460.   
  461.         wanted_spec.userdata = aCodecCtx;   
  462.   
  463.         
  464.   
  465.         if (SDL_OpenAudio(&wanted_spec, &spec) < 0) {   
  466.   
  467.             fprintf(stderr, "SDL_OpenAudio: %s\n", SDL_GetError());   
  468.   
  469.             return -1;   
  470.   
  471.         }   
  472.   
  473.         aCodec = avcodec_find_decoder(aCodecCtx->codec_id);   
  474.   
  475.         if (!aCodec) {   
  476.   
  477.             fprintf(stderr, "Unsupported codec!\n");   
  478.   
  479.             return -1;   
  480.   
  481.         }   
  482.   
  483.         avcodec_open(aCodecCtx, aCodec);   
  484.   
  485.         
  486.   
  487.         packet_queue_init(&audioq);   
  488.   
  489.         SDL_PauseAudio(0);   
  490.   
  491.         
  492.   
  493.         pCodecCtx=pFormatCtx->streams[videoStream]->codec;   
  494.   
  495.         img_convert_ctx = sws_getContext(pCodecCtx->width,   
  496.   
  497.                 pCodecCtx->height,   
  498.   
  499.                 pCodecCtx->pix_fmt,   
  500.   
  501.                 pCodecCtx->width,   
  502.   
  503.                 pCodecCtx->height,   
  504.   
  505.                 PIX_FMT_YUV420P,   
  506.   
  507.                 SWS_BICUBIC,   
  508.   
  509.                 NULL, NULL, NULL);   
  510.   
  511.         
  512.   
  513.         
  514.   
  515.         pCodec = avcodec_find_decoder(pCodecCtx->codec_id);   
  516.   
  517.         if (pCodec == NULL) {   
  518.   
  519.             fprintf(stderr, "Unsupported codec!\n");   
  520.   
  521.             return -1;   
  522.   
  523.         }   
  524.   
  525.         if (avcodec_open(pCodecCtx, pCodec) < 0) {   
  526.   
  527.             return -1;   
  528.   
  529.         }   
  530.   
  531.         
  532.   
  533.         pFrame = avcodec_alloc_frame();   
  534.   
  535.         
  536.   
  537.         screen = SDL_SetVideoMode(pCodecCtx->width, pCodecCtx->height, 24, 0);   
  538.   
  539.         
  540.   
  541.         if (!screen) {   
  542.   
  543.             fprintf(stderr, "SDL : could not set video mode - exiting\n");   
  544.   
  545.             exit(1);   
  546.   
  547.         }   
  548.   
  549.         
  550.   
  551.         bmp = SDL_CreateYUVOverlay(pCodecCtx->width,   
  552.   
  553.                            pCodecCtx->height,   
  554.   
  555.                        SDL_YV12_OVERLAY,   
  556.   
  557.                        screen);   
  558.   
  559.         i = 0;   
  560.   
  561.         while(av_read_frame(pFormatCtx, &packet) >= 0) {   
  562.   
  563.             if (packet.stream_index == videoStream) {   
  564.   
  565.                 avcodec_decode_video(pCodecCtx, pFrame, &frameFinished,   
  566.   
  567.                         packet.data, packet.size);   
  568.   
  569.         
  570.   
  571.                 if (frameFinished) {   
  572.   
  573.                     SDL_LockYUVOverlay(bmp);   
  574.   
  575.         
  576.   
  577.                     AVPicture pict;   
  578.   
  579.                     pict.data[0] = bmp->pixels[0];   
  580.   
  581.                     pict.data[1] = bmp->pixels[2];   
  582.   
  583.                     pict.data[2] = bmp->pixels[1];   
  584.   
  585.         
  586.   
  587.                     pict.linesize[0] = bmp->pitches[0];   
  588.   
  589.                     pict.linesize[1] = bmp->pitches[2];   
  590.   
  591.                     pict.linesize[2] = bmp->pitches[1];   
  592.   
  593.         
  594.   
  595.                     sws_scale(img_convert_ctx,   
  596.   
  597.                             pFrame->data,   
  598.   
  599.                             pFrame->linesize, 0,   
  600.   
  601.                             pCodecCtx->height,   
  602.   
  603.                             pict.data,   
  604.   
  605.                             pict.linesize);   
  606.   
  607.         
  608.   
  609.                     SDL_UnlockYUVOverlay(bmp);   
  610.   
  611.         
  612.   
  613.                     rect.x = 0;   
  614.   
  615.                     rect.y = 0;   
  616.   
  617.                     rect.w = pCodecCtx->width;   
  618.   
  619.                     rect.h = pCodecCtx->height;   
  620.   
  621.                     SDL_DisplayYUVOverlay(bmp, &rect);   
  622.   
  623.                     av_free_packet(&packet);   
  624.   
  625.                 }   
  626.   
  627.             } else if (packet.stream_index == audioStream) {   
  628.   
  629.                 packet_queue_put(&audioq, &packet);   
  630.   
  631.             } else {   
  632.   
  633.                 av_free_packet(&packet);   
  634.   
  635.             }   
  636.   
  637.         
  638.   
  639.             SDL_PollEvent(&event);   
  640.   
  641.             switch(event.type) {   
  642.   
  643.                 case SDL_QUIT:   
  644.   
  645.                     quit = 1;   
  646.   
  647.                     SDL_Quit();   
  648.   
  649.                     exit(0);   
  650.   
  651.                     break;   
  652.   
  653.                 default:   
  654.   
  655.                     break;   
  656.   
  657.             }   
  658.   
  659.         }   
  660.   
  661.         
  662.   
  663.         av_free(pFrame);   
  664.   
  665.         
  666.   
  667.         avcodec_close(pCodecCtx);   
  668.   
  669.         
  670.   
  671.         av_close_input_file(pFormatCtx);   
  672.   
  673.         
  674.   
  675.         return 0;   
  676.   
  677.     } 
原创粉丝点击