Chinaunix首页 | 论坛 | 博客
  • 博客访问: 2151618
  • 博文数量: 438
  • 博客积分: 3871
  • 博客等级: 中校
  • 技术积分: 6075
  • 用 户 组: 普通用户
  • 注册时间: 2011-09-10 00:11
个人简介

邮箱: wangcong02345@163.com

文章分类

全部博文(438)

文章存档

2017年(15)

2016年(119)

2015年(91)

2014年(62)

2013年(56)

2012年(79)

2011年(16)

分类: LINUX

2012-10-25 16:12:46

1. 4spawing.c
  1. // tutorial01.c
  2. // Code based on a tutorial by Martin Bohme (boehme@inb.uni-luebeckREMOVETHIS.de)
  3. // Tested on Gentoo, CVS version 5/01/07 compiled with GCC 4.1.1

  4. // A small sample program that shows how to use libavformat and libavcodec to
  5. // read video from a file.
  6. //
  7. // Use
  8. //
  9. // gcc -o tutorial01 tutorial01.c -lavformat -lavcodec -lz
  10. //
  11. // to build (assuming libavformat and libavcodec are correctly installed
  12. // your system).
  13. //
  14. // Run using
  15. //
  16. // tutorial01 myvideofile.mpg
  17. //
  18. // to write the first five frames from "myvideofile.mpg" to disk in PPM
  19. // format.
  20. #include <stdio.h>
  21. #include <libavformat/avformat.h>
  22. #include <libswscale/swscale.h>
  23. #include <libavutil/avstring.h>

  24. #include <SDL.h>
  25. #include <SDL_thread.h>
  26. #define SDL_AUDIO_BUFFER_SIZE 1024
  27. #define MAX_AUDIOQ_SIZE (5 * 16 * 1024)
  28. #define MAX_VIDEOQ_SIZE (5 * 256 * 1024)

  29. #define FF_ALLOC_EVENT (SDL_USEREVENT)
  30. #define FF_REFRESH_EVENT (SDL_USEREVENT + 1)
  31. #define FF_QUIT_EVENT (SDL_USEREVENT + 2)

  32. #define VIDEO_PICTURE_QUEUE_SIZE 1
  33. typedef struct PacketQueue
  34. {
  35.     AVPacketList * first_pkt, *last_pkt;
  36.     int nb_packets;
  37.     int size;
  38.     SDL_mutex *mutex;
  39.     SDL_cond * cond;
  40. }PacketQueue;
  41. typedef struct VideoPicture {
  42.     SDL_Overlay *bmp;
  43.     int width, height; /* source height & width */
  44.     int allocated;
  45. } VideoPicture;

  46. typedef struct VideoState {

  47.     AVFormatContext *pFormatCtx;
  48.     int videoStream, audioStream;
  49.     AVStream *audio_st;
  50.     PacketQueue audioq;
  51.     uint8_t audio_buf[(AVCODEC_MAX_AUDIO_FRAME_SIZE * 3) / 2];
  52.     unsigned int audio_buf_size;
  53.     unsigned int audio_buf_index;
  54.     AVPacket audio_pkt;
  55.     uint8_t *audio_pkt_data;
  56.     int audio_pkt_size;
  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. } VideoState;

  68. SDL_Surface *screen;

  69. /* Since we only have one decoding thread, the Big Struct
  70.    can be global in case we need it. */
  71. VideoState *global_video_state;
  72. void packet_queue_init(PacketQueue *q)
  73. {
  74.     memset(q, 0, sizeof(PacketQueue));
  75.     q->mutex = SDL_CreateMutex();
  76.     q->cond = SDL_CreateCond();
  77. }

  78. int packet_queue_put(PacketQueue *q, AVPacket *pkt)
  79. {

  80.     AVPacketList *pkt1;
  81.     if(av_dup_packet(pkt) < 0)
  82.         return -1;
  83.     pkt1 = av_malloc(sizeof(AVPacketList));
  84.     if (!pkt1)
  85.         return -1;
  86.     pkt1->pkt = *pkt;
  87.     pkt1->next = NULL;

  88.     SDL_LockMutex(q->mutex);

  89.     if (!q->last_pkt)
  90.         q->first_pkt = pkt1;
  91.     else
  92.         q->last_pkt->next = pkt1;
  93.     q->last_pkt = pkt1;
  94.     q->nb_packets++;
  95.     q->size += pkt1->pkt.size;
  96.     SDL_CondSignal(q->cond);

  97.     SDL_UnlockMutex(q->mutex);
  98.     return 0;
  99. }


  100. static int packet_queue_get(PacketQueue *q, AVPacket *pkt, int block)
  101. {
  102.     AVPacketList *pkt1;
  103.     int ret;

  104.     SDL_LockMutex(q->mutex);

  105.     for(;;)
  106.     {

  107.         if(global_video_state->quit)
  108.         {
  109.             ret = -1;
  110.             break;
  111.         }

  112.         pkt1 = q->first_pkt;
  113.         if (pkt1)
  114.         {
  115.             q->first_pkt = pkt1->next;
  116.             if (!q->first_pkt)
  117.                 q->last_pkt = NULL;
  118.             q->nb_packets--;
  119.             q->size -= pkt1->pkt.size;
  120.             *pkt = pkt1->pkt;
  121.             av_free(pkt1);
  122.             ret = 1;
  123.             break;
  124.         } else if (!block) {
  125.             ret = 0;
  126.             break;
  127.         } else {
  128.             SDL_CondWait(q->cond, q->mutex);
  129.         }
  130.     }
  131.     SDL_UnlockMutex(q->mutex);
  132.     return ret;
  133. }


  134. //int audio_decode_frame(AVCodecContext *aCodecCtx, uint8_t *audio_buf, int buf_size) {
  135. //int audio_decode_frame(AVCodecContext *aCodecCtx, AVPacket *pkt, AVPacket *pkt_temp, AVFrame *frame, uint8_t *audio_buf)
  136. int audio_decode_frame(VideoState *is, AVPacket *pkt, AVPacket *pkt_temp, AVFrame *frame, uint8_t *audio_buf)
  137.     //int audio_decode_frame(VideoState *is, uint8_t *audio_buf, int buf_size) {
  138. {
  139.     int len1, data_size, got_frame;
  140.     int new_packet;
  141.     for(;;)
  142.     {
  143.         while(pkt_temp->size>0 || (!pkt_temp->data && new_packet))
  144.         {
  145.             if(!frame)
  146.             {
  147.                 if (!(frame = avcodec_alloc_frame()))
  148.                     return AVERROR(ENOMEM);
  149.             }
  150.             else
  151.             {
  152.                 avcodec_get_frame_defaults(frame);
  153.             }
  154.             new_packet = 0;
  155.             //data_size = buf_size;
  156.             //int avcodec_decode_audio4(AVCodecContext *avctx, AVFrame *frame, int *got_frame_ptr, const AVPacket *avpkt);
  157.             //len1 = avcodec_decode_audio2(aCodecCtx, (int16_t *)audio_buf, &data_size, audio_pkt_data, audio_pkt_size);
  158.             len1 = avcodec_decode_audio4(is->audio_st->codec, frame, &got_frame, pkt_temp);
  159.             if(len1 < 0)
  160.             {
  161.                 /* if error, skip frame */
  162.                 pkt_temp->size = 0;
  163.                 break;
  164.             }
  165.             pkt_temp->data += len1;
  166.             pkt_temp->size -= len1;

  167.             if(got_frame <= 0) /* No data yet, get more frames */
  168.                 continue;
  169.             data_size = av_samples_get_buffer_size(NULL, is->audio_st->codec->channels, frame->nb_samples, is->audio_st->codec->sample_fmt, 1);
  170.             memcpy(audio_buf, frame->data[0], frame->linesize[0]);
  171.             /* We have data, return it and come back for more later */
  172.             return data_size;
  173.         }
  174.         if(pkt->data)
  175.             av_free_packet(pkt);
  176.         memset(pkt_temp, 0, sizeof(*pkt_temp));
  177.         if(is->quit)
  178.             return -1;

  179.         if((new_packet = packet_queue_get(&is->audioq, pkt, 1)) < 0)
  180.             return -1;
  181.         *pkt_temp = *pkt;
  182.     }
  183. }

  184. void audio_callback(void *userdata, Uint8 *stream, int len)
  185. {
  186.     VideoState *is = (VideoState *)userdata;
  187.     //AVCodecContext *aCodecCtx = (AVCodecContext *)userdata;
  188.     int len1, audio_size;

  189.     AVPacket *pkt = av_mallocz(sizeof(AVPacket));
  190.     AVPacket *pkt_temp = av_mallocz(sizeof(AVPacket));
  191.     AVFrame *frame = NULL;

  192.     while(len > 0)
  193.     {
  194.         if(is->audio_buf_index >= is->audio_buf_size)
  195.         {
  196.             /* We have already sent all our data; get more */
  197.             //audio_size = audio_decode_frame(aCodecCtx, audio_buf, sizeof(audio_buf));
  198.             audio_size = audio_decode_frame(is, pkt, pkt_temp, frame, is->audio_buf);
  199.             if(audio_size < 0)
  200.             {
  201.                 /* If error, output silence */
  202.                 is->audio_buf_size = 1024;
  203.                 memset(is->audio_buf, 0, is->audio_buf_size);
  204.             } else {
  205.                 is->audio_buf_size = audio_size;
  206.             }
  207.             is->audio_buf_index = 0;
  208.         }
  209.         len1 = is->audio_buf_size - is->audio_buf_index;
  210.         if(len1 > len)
  211.             len1 = len;
  212.         memcpy(stream, (uint8_t *)is->audio_buf + is->audio_buf_index, len1);
  213.         len -= len1;
  214.         stream += len1;
  215.         is->audio_buf_index += len1;
  216.     }
  217. }

  218. static Uint32 sdl_refresh_timer_cb(Uint32 interval, void *opaque)
  219. {
  220.     SDL_Event event;
  221.     event.type = FF_REFRESH_EVENT;
  222.     event.user.data1 = opaque;
  223.     SDL_PushEvent(&event);
  224.     return 0; /* 0 means stop timer */
  225. }

  226. /* schedule a video refresh in 'delay' ms */
  227. static void schedule_refresh(VideoState *is, int delay)
  228. {
  229.     SDL_AddTimer(delay, sdl_refresh_timer_cb, is);
  230. }

  231. void video_display(VideoState *is)
  232. {

  233.     SDL_Rect rect;
  234.     VideoPicture *vp;
  235.     //AVPicture pict;
  236.     float aspect_ratio;
  237.     int w, h, x, y;

  238.     vp = &is->pictq[is->pictq_rindex];
  239.     if(vp->bmp)
  240.     {
  241.         if(is->video_st->codec->sample_aspect_ratio.num == 0)
  242.         {
  243.             aspect_ratio = 0;
  244.         } else {
  245.             aspect_ratio = av_q2d(is->video_st->codec->sample_aspect_ratio) *
  246.                 is->video_st->codec->width / is->video_st->codec->height;
  247.         }
  248.         if(aspect_ratio <= 0.0)
  249.         {
  250.             aspect_ratio = (float)is->video_st->codec->width /
  251.                 (float)is->video_st->codec->height;
  252.         }
  253.         h = screen->h;
  254.         w = ((int)rint(h * aspect_ratio)) & -3;
  255.         if(w > screen->w)
  256.         {
  257.             w = screen->w;
  258.             h = ((int)rint(w / aspect_ratio)) & -3;
  259.         }
  260.         x = (screen->w - w) / 2;
  261.         y = (screen->h - h) / 2;

  262.         rect.x = x;
  263.         rect.y = y;
  264.         rect.w = w;
  265.         rect.h = h;
  266.         SDL_DisplayYUVOverlay(vp->bmp, &rect);
  267.     }
  268. }

  269. void video_refresh_timer(void *userdata)
  270. {
  271.     VideoState *is = (VideoState *)userdata;
  272.     VideoPicture __attribute__ ((unused))*vp;

  273.     if(is->video_st)
  274.     {
  275.         if(is->pictq_size == 0)
  276.             schedule_refresh(is, 1);
  277.         else
  278.         {
  279.             vp = &is->pictq[is->pictq_rindex];
  280.             /* Now, normally here goes a ton of code
  281.                about timing, etc. we're just going to
  282.                guess at a delay for now. You can
  283.                increase and decrease this value and hard code
  284.                the timing - but I don't suggest that ;)
  285.                We'll learn how to do it for real later.
  286.                */
  287.             schedule_refresh(is, 80);

  288.             /* show the */
  289.             video_display(is);

  290.             /* update queue for next */
  291.             if(++is->pictq_rindex == VIDEO_PICTURE_QUEUE_SIZE)
  292.             {
  293.                 is->pictq_rindex = 0;
  294.             }
  295.             SDL_LockMutex(is->pictq_mutex);
  296.             is->pictq_size--;
  297.             SDL_CondSignal(is->pictq_cond);
  298.             SDL_UnlockMutex(is->pictq_mutex);
  299.         }
  300.     } else {
  301.         schedule_refresh(is, 100);
  302.     }
  303. }

  304. void alloc_picture(void *userdata) {

  305.     VideoState *is = (VideoState *)userdata;
  306.     VideoPicture *vp;

  307.     vp = &is->pictq[is->pictq_windex];
  308.     if(vp->bmp) {
  309.         // we already have one make another, bigger/smaller
  310.         SDL_FreeYUVOverlay(vp->bmp);
  311.     }
  312.     // Allocate a place to put our YUV image on that screen
  313.     vp->bmp = SDL_CreateYUVOverlay(is->video_st->codec->width,
  314.             is->video_st->codec->height,
  315.             SDL_YV12_OVERLAY,
  316.             screen);
  317.     vp->width = is->video_st->codec->width;
  318.     vp->height = is->video_st->codec->height;

  319.     SDL_LockMutex(is->pictq_mutex);
  320.     vp->allocated = 1;
  321.     SDL_CondSignal(is->pictq_cond);
  322.     SDL_UnlockMutex(is->pictq_mutex);

  323. }

  324. int queue_picture(VideoState *is, AVFrame *pFrame)
  325. {
  326.     VideoPicture *vp;
  327.     int __attribute__ ((unused)) dst_pix_fmt;
  328.     AVPicture pict;

  329.     /* wait until we have space for a new pic */
  330.     SDL_LockMutex(is->pictq_mutex);
  331.     while(is->pictq_size >= VIDEO_PICTURE_QUEUE_SIZE && !is->quit)
  332.         SDL_CondWait(is->pictq_cond, is->pictq_mutex);
  333.     
  334.     SDL_UnlockMutex(is->pictq_mutex);

  335.     if(is->quit)
  336.         return -1;

  337.     // windex is set to 0 initially
  338.     vp = &is->pictq[is->pictq_windex];

  339.     /* allocate or resize the */
  340.     if(!vp->bmp || vp->width != is->video_st->codec->width ||
  341.             vp->height != is->video_st->codec->height) {
  342.         SDL_Event event;

  343.         vp->allocated = 0;
  344.         /* we have to do it in the main thread */
  345.         event.type = FF_ALLOC_EVENT;
  346.         event.user.data1 = is;
  347.         SDL_PushEvent(&event);

  348.         /* wait until we have a picture allocated */
  349.         SDL_LockMutex(is->pictq_mutex);
  350.         while(!vp->allocated && !is->quit)
  351.             SDL_CondWait(is->pictq_cond, is->pictq_mutex);

  352.         SDL_UnlockMutex(is->pictq_mutex);
  353.         if(is->quit)
  354.             return -1;
  355.     }
  356.     /* We have a place to put our picture on the queue */

  357.     if(vp->bmp)
  358.     {
  359.         SDL_LockYUVOverlay(vp->bmp);
  360.         dst_pix_fmt = PIX_FMT_YUV420P;

  361.         /* point pict at the queue */
  362.         pict.data[0] = vp->bmp->pixels[0];
  363.         pict.data[1] = vp->bmp->pixels[2];
  364.         pict.data[2] = vp->bmp->pixels[1];

  365.         pict.linesize[0] = vp->bmp->pitches[0];
  366.         pict.linesize[1] = vp->bmp->pitches[2];
  367.         pict.linesize[2] = vp->bmp->pitches[1];

  368.         // Convert the image into YUV format that SDL uses
  369.         //img_convert(&pict, dst_pix_fmt,
  370.         // (AVPicture *)pFrame, is->video_st->codec->pix_fmt,
  371.         // is->video_st->codec->width, is->video_st->codec->height);
  372.         struct SwsContext *img_convert_ctx;
  373.         img_convert_ctx = sws_getContext(is->video_st->codec->width,is->video_st->codec->height, is->video_st->codec->pix_fmt, is->video_st->codec->width, is->video_st->codec->height, PIX_FMT_YUV420P, SWS_BICUBIC, NULL, NULL, NULL);
  374.         if(img_convert_ctx == NULL)
  375.         {
  376.             fprintf(stderr, "Cannot initialize the conversion context!\n");
  377.             exit(1);
  378.         }

  379.         sws_scale(img_convert_ctx, (const uint8_t* const*)pFrame->data, pFrame->linesize, 0, is->video_st->codec->height, pict.data, pict.linesize);

  380.         SDL_UnlockYUVOverlay(vp->bmp);
  381.         /* now we inform our display thread that we have a pic ready */
  382.         if(++is->pictq_windex == VIDEO_PICTURE_QUEUE_SIZE)
  383.             is->pictq_windex = 0;
  384.         SDL_LockMutex(is->pictq_mutex);
  385.         is->pictq_size++;
  386.         SDL_UnlockMutex(is->pictq_mutex);
  387.     }
  388.     return 0;
  389. }

  390. int video_thread(void *arg)
  391. {
  392.     VideoState *is = (VideoState *)arg;
  393.     AVPacket pkt1, *packet = &pkt1;
  394.     int __attribute__ ((unused)) len1, frameFinished;
  395.     AVFrame *pFrame;

  396.     pFrame = avcodec_alloc_frame();

  397.     for(;;)
  398.     {
  399.         if(packet_queue_get(&is->videoq, packet, 1) < 0) // means we quit getting packets
  400.             break;
  401.         // Decode video frame
  402.         len1 = avcodec_decode_video2(is->video_st->codec, pFrame, &frameFinished, packet);

  403.         // Did we get a video frame?
  404.         if(frameFinished)
  405.         {
  406.             if(queue_picture(is, pFrame) < 0)
  407.                 break;
  408.         }
  409.         av_free_packet(packet);
  410.     }
  411.     av_free(pFrame);
  412.     return 0;
  413. }

  414. int stream_component_open(VideoState *is, int stream_index)
  415. {
  416.     AVFormatContext *pFormatCtx = is->pFormatCtx;
  417.     AVCodecContext *codecCtx;
  418.     AVCodec *codec;
  419.     SDL_AudioSpec wanted_spec, spec;

  420.     if(stream_index < 0 || stream_index >= pFormatCtx->nb_streams)
  421.         return -1;

  422.     // Get a pointer to the codec context for the video stream
  423.     codecCtx = pFormatCtx->streams[stream_index]->codec;

  424.     if(codecCtx->codec_type == AVMEDIA_TYPE_AUDIO)
  425.     {
  426.         // Set audio settings from codec info
  427.         wanted_spec.freq = codecCtx->sample_rate;
  428.         wanted_spec.format = AUDIO_S16SYS;
  429.         wanted_spec.channels = codecCtx->channels;
  430.         wanted_spec.silence = 0;
  431.         wanted_spec.samples = SDL_AUDIO_BUFFER_SIZE;
  432.         wanted_spec.callback = audio_callback;
  433.         wanted_spec.userdata = is;

  434.         if(SDL_OpenAudio(&wanted_spec, &spec) < 0)
  435.         {
  436.             fprintf(stderr, "SDL_OpenAudio: %s\n", SDL_GetError());
  437.             return -1;
  438.         }
  439.     }
  440.     codec = avcodec_find_decoder(codecCtx->codec_id);
  441.     if(!codec || (avcodec_open2(codecCtx, codec, NULL) < 0))
  442.     {
  443.         fprintf(stderr, "Unsupported codec!\n");
  444.         return -1;
  445.     }

  446.     switch(codecCtx->codec_type)
  447.     {
  448.         case AVMEDIA_TYPE_AUDIO:
  449.             is->audioStream = stream_index;
  450.             is->audio_st = pFormatCtx->streams[stream_index];
  451.             is->audio_buf_size = 0;
  452.             is->audio_buf_index = 0;
  453.             memset(&is->audio_pkt, 0, sizeof(is->audio_pkt));
  454.             packet_queue_init(&is->audioq);
  455.             SDL_PauseAudio(0);
  456.             break;
  457.         case AVMEDIA_TYPE_VIDEO:
  458.             is->videoStream = stream_index;
  459.             is->video_st = pFormatCtx->streams[stream_index];
  460.             packet_queue_init(&is->videoq);
  461.             is->video_tid = SDL_CreateThread(video_thread, is);
  462.             break;
  463.         default:
  464.             break;
  465.     }
  466.     return 0;
  467. }

  468. int decode_interrupt_cb(void)
  469. {
  470.     return (global_video_state && global_video_state->quit);
  471. }

  472. int decode_thread(void *arg)
  473. {

  474.     VideoState *is = (VideoState *)arg;
  475.     AVFormatContext *pFormatCtx;
  476.     AVPacket pkt1, *packet = &pkt1;

  477.     int video_index = -1;
  478.     int audio_index = -1;
  479.     int i;

  480.     is->videoStream=-1;
  481.     is->audioStream=-1;

  482.     global_video_state = is;
  483.     // will interrupt blocking functions if we
  484.     //url_set_interrupt_cb(decode_interrupt_cb);

  485.     pFormatCtx = avformat_alloc_context();
  486.     // Open video file
  487.     if(avformat_open_input(&pFormatCtx, is->filename, NULL, NULL)!=0)
  488.         return -1; // Couldn't open file

  489.     is->pFormatCtx = pFormatCtx;

  490.     // Retrieve stream information
  491.     if(avformat_find_stream_info(pFormatCtx, NULL)<0)
  492.         return -1; // Couldn't find stream information

  493.     // Dump information about file onto standard error
  494.     av_dump_format(pFormatCtx, 0, is->filename, 0);

  495.     // Find the first video stream

  496.     for(i=0; i<pFormatCtx->nb_streams; i++)
  497.     {
  498.         if(pFormatCtx->streams[i]->codec->codec_type==AVMEDIA_TYPE_VIDEO && video_index < 0)
  499.             video_index=i;
  500.         if(pFormatCtx->streams[i]->codec->codec_type==AVMEDIA_TYPE_AUDIO && audio_index < 0)
  501.             audio_index=i;
  502.     }
  503.     if(audio_index >= 0)
  504.         stream_component_open(is, audio_index);
  505.     if(video_index >= 0)
  506.         stream_component_open(is, video_index);

  507.     if(is->videoStream < 0 || is->audioStream < 0)
  508.     {
  509.         fprintf(stderr, "%s: could not open codecs\n", is->filename);
  510.         goto fail;
  511.     }

  512.     // main decode loop

  513.     for(;;)
  514.     {
  515.         if(is->quit)
  516.             break;
  517.         // seek stuff goes here
  518.         if(is->audioq.size > MAX_AUDIOQ_SIZE || is->videoq.size > MAX_VIDEOQ_SIZE)
  519.         {
  520.             SDL_Delay(10);
  521.             continue;
  522.         }
  523.         if(av_read_frame(is->pFormatCtx, packet) < 0)
  524.         {
  525.             //if(url_ferror(&pFormatCtx->pb) == 0)
  526.             if(pFormatCtx->pb&&pFormatCtx->pb->error)
  527.             {
  528.                 SDL_Delay(100); /* no error; wait for user input */
  529.                 continue;
  530.             } else {
  531.                 break;
  532.             }
  533.         }
  534.         // Is this a packet from the video stream?
  535.         if(packet->stream_index == is->videoStream)
  536.             packet_queue_put(&is->videoq, packet);
  537.         else if(packet->stream_index == is->audioStream)
  538.             packet_queue_put(&is->audioq, packet);
  539.         else
  540.             av_free_packet(packet);
  541.     }
  542.     /* all done - wait for it */
  543.     while(!is->quit)
  544.         SDL_Delay(100);

  545. fail:
  546.     if(1){
  547.         SDL_Event event;
  548.         event.type = FF_QUIT_EVENT;
  549.         event.user.data1 = is;
  550.         SDL_PushEvent(&event);
  551.     }
  552.     return 0;
  553. }

  554. int main(int argc, char *argv[])
  555. {
  556.     SDL_Event event;
  557.     VideoState *is;
  558.     is = av_mallocz(sizeof(VideoState));

  559.     if(argc < 2)
  560.     {
  561.         fprintf(stderr, "Usage: test \n");
  562.         exit(1);
  563.     }
  564.     // Register all formats and codecs
  565.     av_register_all();

  566.     if(SDL_Init(SDL_INIT_VIDEO | SDL_INIT_AUDIO | SDL_INIT_TIMER))
  567.     {
  568.         fprintf(stderr, "Could not initialize SDL - %s\n", SDL_GetError());
  569.         exit(1);
  570.     }

  571.     // Make a screen to put our video
  572. #ifndef __DARWIN__
  573.     screen = SDL_SetVideoMode(640, 480, 0, 0);
  574. #else
  575.     screen = SDL_SetVideoMode(640, 480, 24, 0);
  576. #endif
  577.     if(!screen)
  578.     {
  579.         fprintf(stderr, "SDL: could not set video mode - exiting\n");
  580.         exit(1);
  581.     }

  582.     //void pstrcpy(char *dst, size_t size, const char *src)
  583.     //pstrcpy(is->filename, sizeof(is->filename), argv[1]);

  584.     //size_t av_strlcpy(char *dst, const char *src, size_t size)
  585.     av_strlcpy(is->filename, argv[1], sizeof(is->filename));

  586.     is->pictq_mutex = SDL_CreateMutex();
  587.     is->pictq_cond = SDL_CreateCond();

  588.     schedule_refresh(is, 40);

  589.     is->parse_tid = SDL_CreateThread(decode_thread, is);
  590.     if(!is->parse_tid)
  591.     {
  592.         av_free(is);
  593.         return -1;
  594.     }
  595.     for(;;)
  596.     {
  597.         SDL_WaitEvent(&event);
  598.         switch(event.type)
  599.         {
  600.             case FF_QUIT_EVENT:
  601.             case SDL_QUIT:
  602.                 is->quit = 1;
  603.                 SDL_Quit();
  604.                 return 0;
  605.                 break;
  606.             case FF_ALLOC_EVENT:
  607.                 alloc_picture(event.user.data1);
  608.                 break;
  609.             case FF_REFRESH_EVENT:
  610.                 video_refresh_timer(event.user.data1);
  611.                 break;
  612.             default:
  613.                 break;
  614.         }
  615.     }
  616.     return 0;
  617. }
2. Makefile
  1. CC=gcc
  2. CFLAGS = -Wall -g -I/home/sun/code/ffmpeg-1.0/install/include/ -I/home/sun/code/SDL-1.2.15/install/include/SDL/
  3. LDFLAGS = -L/home/sun/code/ffmpeg-1.0/install/lib/ -lavutil -lavformat -lavcodec -lavutil -lm -lswscale
  4. LDFLAGS += -L/home/sun/code/SDL-1.2.15/install/lib/ -lSDLmain -lSDL

  5. TARGETS=4spawning
  6. all: $(TARGETS)
  7. 4spawning.o:4spawning.c
  8.     $(CC) $(CFLAGS) -o $@ -c $^

  9. 4spawning:4spawning.o
  10.     $(CC) -o $@ $^ $(LDFLAGS)
  11. clean:
  12.     rm -rf *.o $(TARGETS)

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