Chinaunix首页 | 论坛 | 博客
  • 博客访问: 1148512
  • 博文数量: 173
  • 博客积分: 4048
  • 博客等级:
  • 技术积分: 2679
  • 用 户 组: 普通用户
  • 注册时间: 2010-09-12 18:53
文章分类

全部博文(173)

文章存档

2018年(1)

2016年(1)

2013年(1)

2012年(118)

2011年(52)

分类: 嵌入式

2012-04-17 14:18:51

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


阅读(1807) | 评论(0) | 转发(1) |
给主人留下些什么吧!~~