Subversion Repositories Kolibri OS

Rev

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