Subversion Repositories Kolibri OS

Rev

Rev 1696 | Blame | Last modification | View Log | Download | RSS feed

  1.  
  2. #include <stdint.h>
  3.  
  4. #include <libavcodec/avcodec.h>
  5. #include <libavformat/avformat.h>
  6. #include <libavdevice/avdevice.h>
  7. #include <libswscale/swscale.h>
  8.  
  9. #include <stdio.h>
  10. #include <string.h>
  11. #include <fcntl.h>
  12. #include <ctype.h>
  13.  
  14. #include "sound.h"
  15. #include "fplay.h"
  16.  
  17. volatile uint32_t status = 1;
  18.  
  19. uint32_t win_width, win_height;
  20.  
  21. void decoder();
  22.  
  23. AVFormatContext *pFormatCtx;
  24. AVCodecContext  *pCodecCtx;
  25. AVCodecContext  *aCodecCtx;
  26. AVCodec         *pCodec;
  27. AVCodec         *aCodec;
  28. AVFrame         *pFrame;
  29. int             videoStream;
  30. int             audioStream;
  31.  
  32. int             have_sound = 0;
  33.  
  34. uint8_t     *decoder_buffer;
  35. extern int sample_rate;
  36. char *movie_file;
  37.  
  38.  
  39. queue_t  q_video;
  40. queue_t  q_audio;
  41.  
  42. int main( int argc, char *argv[])
  43. {
  44.     int i;
  45.  
  46.     if(argc < 2) {
  47.         printf("Please provide a movie file\n");
  48.         return -1;
  49.     }
  50.  
  51.     movie_file = argv[1];
  52.     /* register all codecs, demux and protocols */
  53.  
  54.     avcodec_register_all();
  55.     avdevice_register_all();
  56.     av_register_all();
  57.  
  58.     if( avformat_open_input(&pFormatCtx, argv[1], NULL, NULL) < 0)
  59.     {
  60.         printf("Cannot open file %s\n\r", argv[1]);
  61.         return -1; // Couldn't open file
  62.     };
  63.  
  64.     printf("%s\n\r", __FUNCTION__);
  65.  
  66.   // Retrieve stream information
  67.     if(avformat_find_stream_info(pFormatCtx, NULL)<0)
  68.     {
  69.         printf("Cannot find streams\n\r");
  70.         return -1;
  71.     };
  72.  
  73. //    __asm__ __volatile__("int3");
  74.  
  75.  // dump_format(pFormatCtx, 0, argv[1], 0);
  76.  
  77.    // Find the first video stream
  78.     videoStream=-1;
  79.     audioStream=-1;
  80.     for(i=0; i < pFormatCtx->nb_streams; i++)
  81.     {
  82.         if(pFormatCtx->streams[i]->codec->codec_type==AVMEDIA_TYPE_VIDEO
  83.             && videoStream < 0)
  84.         {
  85.             videoStream=i;
  86.             video_time_base = pFormatCtx->streams[i]->time_base;
  87.  
  88.         }
  89.         if(pFormatCtx->streams[i]->codec->codec_type==AVMEDIA_TYPE_AUDIO &&
  90.             audioStream < 0)
  91.         {
  92.             audioStream=i;
  93.         }
  94.     }
  95.  
  96.     if(videoStream==-1)
  97.     {
  98.         printf("Video stream not detected\n\r");
  99.         return -1; // Didn't find a video stream
  100.     }
  101.  
  102.  //   __asm__ __volatile__("int3");
  103.  
  104.     // Get a pointer to the codec context for the video stream
  105.     pCodecCtx=pFormatCtx->streams[videoStream]->codec;
  106.     aCodecCtx=pFormatCtx->streams[audioStream]->codec;
  107.  
  108.   // Find the decoder for the video stream
  109.     pCodec=avcodec_find_decoder(pCodecCtx->codec_id);
  110.     if(pCodec==NULL) {
  111.         printf("Unsupported codec with id %d for input stream %d\n",
  112.         pCodecCtx->codec_id, videoStream);
  113.         return -1; // Codec not found
  114.     }
  115.  
  116.     if(avcodec_open2(pCodecCtx, pCodec, NULL) < 0)
  117.     {
  118.         printf("Error while opening codec for input stream %d\n",
  119.                 videoStream);
  120.         return -1; // Could not open codec
  121.     };
  122.  
  123.     if (aCodecCtx->channels > 0)
  124.             aCodecCtx->request_channels = FFMIN(2, aCodecCtx->channels);
  125.     else
  126.             aCodecCtx->request_channels = 2;
  127.  
  128.     aCodec = avcodec_find_decoder(aCodecCtx->codec_id);
  129.  
  130.     if(aCodec)
  131.     {
  132.         if(avcodec_open2(aCodecCtx, aCodec, NULL) >= 0 )
  133.         {
  134.             WAVEHEADER       whdr;
  135.             int fmt;
  136.  
  137.             printf("audio stream rate %d channels %d\n",
  138.             aCodecCtx->sample_rate, aCodecCtx->channels);
  139.  
  140.             whdr.riff_id = 0x46464952;
  141.             whdr.riff_format = 0x45564157;
  142.             whdr.wFormatTag = 0x01;
  143.             whdr.nSamplesPerSec = aCodecCtx->sample_rate;
  144.             whdr.nChannels = aCodecCtx->channels;
  145.             whdr.wBitsPerSample = 16;
  146.  
  147.             sample_rate = aCodecCtx->sample_rate;
  148.  
  149.             fmt = test_wav(&whdr);
  150.  
  151.             if( init_audio(fmt) )
  152.             {
  153.                 decoder_buffer = (uint8_t*)av_mallocz(AVCODEC_MAX_AUDIO_FRAME_SIZE*2+64);
  154.                 if( decoder_buffer != NULL )
  155.                 {
  156.                     astream.lock   = 0;
  157.                     astream.count  = 0;
  158.                     astream.buffer = (char *)av_mallocz(AVCODEC_MAX_AUDIO_FRAME_SIZE*8);
  159.                     if( astream.buffer != NULL )
  160.                         have_sound = 1;
  161.                     else
  162.                         av_free(decoder_buffer);
  163.                 }
  164.                 if( have_sound == 0)
  165.                 {
  166.                         printf("Not enough memory for audio buffers\n");
  167.                 }
  168.             }
  169.         }
  170.         else printf("Cannot open audio codec\n\r");
  171.     }
  172.     else printf("Unsupported audio codec!\n");
  173.  
  174.     if( !init_video(pCodecCtx))
  175.         return 0;
  176.  
  177. //    __asm__ __volatile__("int3");
  178.  
  179.     decoder();
  180.  
  181.   // Free the YUV frame
  182.     av_free(pFrame);
  183.  
  184.  
  185. //__asm__ __volatile__("int3");
  186.  
  187.   // Close the codec
  188.  // avcodec_close(pCodecCtx);
  189.  
  190.   // Close the video file
  191.  // av_close_input_file(pFormatCtx);
  192.  
  193. //__asm__ __volatile__("int3");
  194.  
  195.     return 0;
  196. }
  197.  
  198. void decoder()
  199. {
  200.     int       eof = 0;
  201.     AVPacket  packet;
  202.     int       ret;
  203.  
  204.     while( status != 0 && !eof)
  205.     {
  206.         int err;
  207.  
  208. //        __asm__ __volatile__("int3");
  209.  
  210.         if(q_video.size+q_audio.size < 16*1024*1024)
  211.         {
  212.             err = av_read_frame(pFormatCtx, &packet);
  213.             if( err < 0)
  214.             {
  215.                 eof = 1;
  216.                 if (err != AVERROR_EOF)
  217.                     printf("av_read_frame: error %x\n", err);
  218.                 continue;
  219.             }
  220.             if(packet.stream_index==videoStream)
  221.             {
  222.                 put_packet(&q_video, &packet);
  223.             }
  224.             else if( (packet.stream_index == audioStream) &&
  225.                  (have_sound != 0) )
  226.             {
  227.                 put_packet(&q_audio, &packet);
  228.             }
  229.             else
  230.             {
  231.                 av_free_packet(&packet);
  232.             };
  233.             decode_video(pCodecCtx, &q_video);
  234.             decode_audio(aCodecCtx, &q_audio);
  235.             continue;
  236.         };
  237.         decode_video(pCodecCtx, &q_video);
  238.         decode_audio(aCodecCtx, &q_audio);
  239.         delay(1);
  240.     };
  241.  
  242.     ret = 1;
  243.  
  244.     while(status != 0 && ret)
  245.     {
  246.         ret =  decode_video(pCodecCtx, &q_video);
  247.         ret |= decode_audio(aCodecCtx, &q_audio);
  248.         delay(1);
  249.     };
  250.     delay(50);
  251.     status = 0;
  252.     printf("status = 0\n");
  253.     delay(200);
  254. };
  255.  
  256.