ffmpeg 0.6.3 能用的tutorial03

编译totorial03.c的命令为:
gcc -o tutorial03 tutorial03.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/SDL.h> 
  6.     #include <SDL/SDL_thread.h> 
  7.       
  8.     #include <stdio.h> 
  9.       
  10.     #define SDL_AUDIO_BUFFER_SIZE 1024 
  11.       
  12.       
  13.     typedef struct PacketQueue { 
  14.         AVPacketList *first_pkt, *last_pkt; 
  15.         int nb_packets; 
  16.         int size; 
  17.         SDL_mutex *mutex; 
  18.         SDL_cond *cond; 
  19.     } PacketQueue; 
  20.       
  21.     PacketQueue audioq; 
  22.       
  23.     int quit = 0; 
  24.       
  25.     void packet_queue_init(PacketQueue *q) { 
  26.         memset(q, 0, sizeof(PacketQueue)); 
  27.         q->mutex = SDL_CreateMutex(); 
  28.         q->cond = SDL_CreateCond(); 
  29.     } 
  30.       
  31.     int packet_queue_put(PacketQueue *q, AVPacket *pkt) { 
  32.         AVPacketList *pkt1; 
  33.         if (av_dup_packet(pkt) < 0) { 
  34.             return -1; 
  35.         } 
  36.         pkt1 = av_malloc(sizeof(AVPacketList)); 
  37.         if (!pkt1) { 
  38.             return -1; 
  39.         } 
  40.         pkt1->pkt = *pkt; 
  41.         pkt1->next = NULL; 
  42.       
  43.         SDL_LockMutex(q->mutex); 
  44.       
  45.         if (!q->last_pkt) { 
  46.             q->first_pkt  pkt1; 
  47.         } else { 
  48.             q->last_pkt->next = pkt1; 
  49.         } 
  50.         q->last_pkt = pkt1; 
  51.         q->nb_packets++; 
  52.         q->size += pkt1->pkt.size; 
  53.         SDL_CondSignal(q->cond); 
  54.       
  55.         SDL_UnlockMutex(q->mutex); 
  56.         return 0; 
  57.     } 
  58.       
  59.     static int packet_queue_get(PacketQueue *q, AVPacket *pkt, int block) 
  60.     { 
  61.         AVPacketList *pkt1; 
  62.         int ret; 
  63.       
  64.         SDL_LockMutex(q->mutex); 
  65.       
  66.         for (;;) { 
  67.             if (quit) { 
  68.                 ret = -1; 
  69.                 break; 
  70.             } 
  71.       
  72.             pkt1 = q->first_pkt; 
  73.             if (pkt1) { 
  74.                 q->first_pkt = pkt1->next; 
  75.                 if (!q->first_pkt) { 
  76.                     q->last_pkt = NULL; 
  77.                 } 
  78.                 q->nb_packets--; 
  79.                 q->size -= pkt1->pkt.size; 
  80.                 *pkt = pkt1->pkt; 
  81.                 av_free(pkt1); 
  82.                 ret = 1; 
  83.                 break; 
  84.             } else if (!block) { 
  85.                 ret = 0; 
  86.                 break; 
  87.             } else { 
  88.                 SDL_CondWait(q->cond, q->mutex); 
  89.             } 
  90.         } 
  91.         SDL_UnlockMutex(q->mutex); 
  92.         return ret; 
  93.     } 
  94.       
  95.     int audio_decode_frame(AVCodecContext *aCodecCtx, uint8_t *audio_buf, int buf_size) 
  96.     { 
  97.         static AVPacket pkt; 
  98.         static uint8_t *audio_pkt_data = NULL; 
  99.         static int audio_pkt_size = 0; 
  100.       
  101.         int len1, data_size; 
  102.       
  103.         for (;;) { 
  104.             while(audio_pkt_size > 0) { 
  105.                 data_size = buf_size; 
  106.                 len1 = avcodec_decode_audio2(aCodecCtx, (int16_t*)audio_buf, &data_size, audio_pkt_data, audio_pkt_size); 
  107.                 if (len1 < 0) { 
  108.                     audio_pkt_size = 0; 
  109.                     break; 
  110.                 } 
  111.                 audio_pkt_data += len1; 
  112.                 audio_pkt_size -= len1; 
  113.                 if (data_size <= 0) { 
  114.                     continue; 
  115.                 } 
  116.                 return data_size; 
  117.             } 
  118.             if (pkt.data) { 
  119.                 av_free_packet(&pkt); 
  120.             } 
  121.             if (quit) { 
  122.                 return -1; 
  123.             } 
  124.       
  125.             if (packet_queue_get(&audioq, &pkt, 1) < 0) { 
  126.                 return -1; 
  127.             } 
  128.             audio_pkt_data = pkt.data; 
  129.             audio_pkt_size = pkt.size; 
  130.         } 
  131.     } 
  132.       
  133.     void audio_callback(void *userdata, Uint8 *stream, int len) 
  134.     { 
  135.         AVCodecContext *aCodecCtx = (AVCodecContext *)userdata; 
  136.         int len1, audio_size; 
  137.       
  138.         static uint8_t audio_buf[(AVCODEC_MAX_AUDIO_FRAME_SIZE * 3) / 2]; 
  139.         static unsigned int audio_buf_size = 0; 
  140.         static unsigned int audio_buf_index = 0; 
  141.       
  142.         while (len > 0) { 
  143.             if (audio_buf_index >= audio_buf_size) { 
  144.                 audio_size = audio_decode_frame(aCodecCtx, audio_buf, sizeof(audio_buf)); 
  145.                 if (audio_size < 0) { 
  146.                     audio_buf_size = 1024; 
  147.                     memset(audio_buf, 0, audio_buf_size); 
  148.                 } else { 
  149.                     audio_buf_size = audio_size; 
  150.                 } 
  151.                 audio_buf_index = 0; 
  152.             } 
  153.             len1 = audio_buf_size - audio_buf_index; 
  154.             if (len1 > len) { 
  155.                 len1 = len; 
  156.             } 
  157.             memcpy(stream, (uint8_t*)audio_buf + audio_buf_index, len1); 
  158.             len -= len1; 
  159.             stream += len1; 
  160.             audio_buf_index += len1; 
  161.         } 
  162.     } 
  163.       
  164.     int main(int argc, char *argv[]) { 
  165.         AVFormatContext *pFormatCtx; 
  166.         int i, videoStream, audioStream; 
  167.         AVCodecContext *pCodecCtx; 
  168.         AVCodec *pCodec; 
  169.         AVFrame *pFrame; 
  170.         AVPacket packet; 
  171.         int frameFinished; 
  172.         float aspect_ratio; 
  173.         struct SwsContext *img_convert_ctx; 
  174.       
  175.         AVCodecContext *aCodecCtx; 
  176.         AVCodec *aCodec; 
  177.       
  178.         SDL_Overlay *bmp; 
  179.         SDL_Surface *screen; 
  180.         SDL_Rect rect; 
  181.         SDL_Event event; 
  182.         SDL_AudioSpec wanted_spec, spec; 
  183.       
  184.         if (argc < 2) { 
  185.             fprintf(stderr, "Usage: test <file>\n"); 
  186.             exit(1); 
  187.         } 
  188.         av_register_all(); 
  189.       
  190.         if (SDL_Init(SDL_INIT_VIDEO | SDL_INIT_AUDIO | SDL_INIT_TIMER)) { 
  191.             fprintf(stderr, "Could not initialize SDL - %s\n", SDL_GetError()); 
  192.             exit(1); 
  193.         } 
  194.       
  195.         if (av_open_input_file(&pFormatCtx, argv[1], NULL, 0, NULL) != 0) { 
  196.             return -1; 
  197.         } 
  198.       
  199.         if (av_find_stream_info(pFormatCtx) < 0) { 
  200.             return -1; 
  201.         } 
  202.       
  203.         dump_format(pFormatCtx, 0, argv[1], 0); 
  204.       
  205.         videoStream = -1; 
  206.         audioStream = -1; 
  207.         for (i=0; i<pFormatCtx->nb_streams; i++) { 
  208.             if (pFormatCtx->streams[i]->codec->codec_type==CODEC_TYPE_VIDEO && videoStream < 0) { 
  209.                 videoStream = i; 
  210.             } 
  211.             if (pFormatCtx->streams[i]->codec->codec_type == CODEC_TYPE_AUDIO && audioStream < 0) { 
  212.                 audioStream = i; 
  213.             } 
  214.         } 
  215.       
  216.         if (videoStream == -1) { 
  217.             return -1; 
  218.         } 
  219.         if (audioStream == -1) { 
  220.             return -1; 
  221.         } 
  222.       
  223.         aCodecCtx = pFormatCtx->streams[audioStream]->codec; 
  224.       
  225.         wanted_spec.freq = aCodecCtx->sample_rate; 
  226.         wanted_spec.format = AUDIO_S16SYS; 
  227.         wanted_spec.channels = aCodecCtx->channels; 
  228.         wanted_spec.silence = 0; 
  229.         wanted_spec.samples = SDL_AUDIO_BUFFER_SIZE; 
  230.         wanted_spec.callback = audio_callback; 
  231.         wanted_spec.userdata = aCodecCtx; 
  232.       
  233.         if (SDL_OpenAudio(&wanted_spec, &spec) < 0) { 
  234.             fprintf(stderr, "SDL_OpenAudio: %s\n", SDL_GetError()); 
  235.             return -1; 
  236.         } 
  237.         aCodec = avcodec_find_decoder(aCodecCtx->codec_id); 
  238.         if (!aCodec) { 
  239.             fprintf(stderr, "Unsupported codec!\n"); 
  240.             return -1; 
  241.         } 
  242.         avcodec_open(aCodecCtx, aCodec); 
  243.       
  244.         packet_queue_init(&audioq); 
  245.         SDL_PauseAudio(0); 
  246.       
  247.         pCodecCtx=pFormatCtx->streams[videoStream]->codec; 
  248.         img_convert_ctx = sws_getContext(pCodecCtx->width, 
  249.                 pCodecCtx->height, 
  250.                 pCodecCtx->pix_fmt, 
  251.                 pCodecCtx->width, 
  252.                 pCodecCtx->height, 
  253.                 PIX_FMT_YUV420P, 
  254.                 SWS_BICUBIC, 
  255.                 NULL, NULL, NULL); 
  256.       
  257.       
  258.         pCodec = avcodec_find_decoder(pCodecCtx->codec_id); 
  259.         if (pCodec == NULL) { 
  260.             fprintf(stderr, "Unsupported codec!\n"); 
  261.             return -1; 
  262.         } 
  263.         if (avcodec_open(pCodecCtx, pCodec) < 0) { 
  264.             return -1; 
  265.         } 
  266.       
  267.         pFrame = avcodec_alloc_frame(); 
  268.       
  269.         screen = SDL_SetVideoMode(pCodecCtx->width, pCodecCtx->height, 24, 0); 
  270.       
  271.         if (!screen) { 
  272.             fprintf(stderr, "SDL : could not set video mode - exiting\n"); 
  273.             exit(1); 
  274.         } 
  275.       
  276.         bmp = SDL_CreateYUVOverlay(pCodecCtx->width, 
  277.                            pCodecCtx->height, 
  278.                        SDL_YV12_OVERLAY, 
  279.                        screen); 
  280.         i = 0; 
  281.         while(av_read_frame(pFormatCtx, &packet) >= 0) { 
  282.             if (packet.stream_index == videoStream) { 
  283.                 avcodec_decode_video(pCodecCtx, pFrame, &frameFinished, 
  284.                         packet.data, packet.size); 
  285.       
  286.                 if (frameFinished) { 
  287.                     SDL_LockYUVOverlay(bmp); 
  288.       
  289.                     AVPicture pict; 
  290.                     pict.data[0] = bmp->pixels[0]; 
  291.                     pict.data[1] = bmp->pixels[2]; 
  292.                     pict.data[2] = bmp->pixels[1]; 
  293.       
  294.                     pict.linesize[0] = bmp->pitches[0]; 
  295.                     pict.linesize[1] = bmp->pitches[2]; 
  296.                     pict.linesize[2] = bmp->pitches[1]; 
  297.       
  298.                     sws_scale(img_convert_ctx, 
  299.                             pFrame->data, 
  300.                             pFrame->linesize, 0, 
  301.                             pCodecCtx->height, 
  302.                             pict.data, 
  303.                             pict.linesize); 
  304.       
  305.                     SDL_UnlockYUVOverlay(bmp); 
  306.       
  307.                     rect.= 0; 
  308.                     rect.= 0; 
  309.                     rect.= pCodecCtx->width; 
  310.                     rect.= pCodecCtx->height; 
  311.                     SDL_DisplayYUVOverlay(bmp, &rect); 
  312.                     av_free_packet(&packet); 
  313.                 } 
  314.             } else if (packet.stream_index == audioStream) { 
  315.                 packet_queue_put(&audioq, &packet); 
  316.             } else { 
  317.                 av_free_packet(&packet); 
  318.             } 
  319.       
  320.             SDL_PollEvent(&event); 
  321.             switch(event.type) { 
  322.                 case SDL_QUIT: 
  323.                     quit = 1; 
  324.                     SDL_Quit(); 
  325.                     exit(0); 
  326.                     break; 
  327.                 default: 
  328.                     break; 
  329.             } 
  330.         } 
  331.       
  332.         av_free(pFrame); 
  333.       
  334.         avcodec_close(pCodecCtx); 
  335.       
  336.         av_close_input_file(pFormatCtx); 
  337.       
  338.         return 0; 
  339.     }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
资源包主要包含以下内容: ASP项目源码:每个资源包中都包含完整的ASP项目源码,这些源码采用了经典的ASP技术开发,结构清晰、注释详细,帮助用户轻松理解整个项目的逻辑和实现方式。通过这些源码,用户可以学习到ASP的基本语法、服务器端脚本编写方法、数据库操作、用户权限管理等关键技术。 数据库设计文件:为了方便用户更好地理解系统的后台逻辑,每个项目中都附带了完整的数据库设计文件。这些文件通常包括数据库结构图、数据表设计文档,以及示例数据SQL脚本。用户可以通过这些文件快速搭建项目所需的数据库环境,并了解各个数据表之间的关系和作用。 详细的开发文档:每个资源包都附有详细的开发文档,文档内容包括项目背景介绍、功能模块说明、系统流程图、用户界面设计以及关键代码解析等。这些文档为用户提供了深入的学习材料,使得即便是从零开始的开发者也能逐步掌握项目开发的全过程。 项目演示与使用指南:为帮助用户更好地理解和使用这些ASP项目,每个资源包中都包含项目的演示文件和使用指南。演示文件通常以视频或图文形式展示项目的主要功能和操作流程,使用指南则详细说明了如何配置开发环境、部署项目以及常见问题的解决方法。 毕业设计参考:对于正在准备毕业设计的学生来说,这些资源包是绝佳的参考材料。每个项目不仅功能完善、结构清晰,还符合常见的毕业设计要求和标准。通过这些项目,学生可以学习到如何从零开始构建一个完整的Web系统,并积累丰富的项目经验。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值