ffmpeg 0.6.3 能用的tutorial04



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

点击(此处)折叠或打开

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值