Subversion Repositories Kolibri OS

Rev

Rev 2349 | 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.h>
  14.  
  15. #include "sound.h"
  16. #include "fplay.h"
  17.  
  18. volatile uint32_t status = 1;
  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.  //   __asm__ __volatile__("int3");
  106.  
  107.     // Get a pointer to the codec context for the video stream
  108.     pCodecCtx=pFormatCtx->streams[videoStream]->codec;
  109.     aCodecCtx=pFormatCtx->streams[audioStream]->codec;
  110.  
  111.   // Find the decoder for the video stream
  112.     pCodec=avcodec_find_decoder(pCodecCtx->codec_id);
  113.     if(pCodec==NULL) {
  114.         printf("Unsupported codec with id %d for input stream %d\n",
  115.         pCodecCtx->codec_id, videoStream);
  116.         return -1; // Codec not found
  117.     }
  118.  
  119.     if(avcodec_open2(pCodecCtx, pCodec, NULL) < 0)
  120.     {
  121.         printf("Error while opening codec for input stream %d\n",
  122.                 videoStream);
  123.         return -1; // Could not open codec
  124.     };
  125.  
  126.     if (aCodecCtx->channels > 0)
  127.             aCodecCtx->request_channels = FFMIN(2, aCodecCtx->channels);
  128.     else
  129.             aCodecCtx->request_channels = 2;
  130.  
  131.     aCodec = avcodec_find_decoder(aCodecCtx->codec_id);
  132.  
  133.     if(aCodec)
  134.     {
  135.         if(avcodec_open2(aCodecCtx, aCodec, NULL) >= 0 )
  136.         {
  137.             WAVEHEADER       whdr;
  138.             int fmt;
  139.  
  140.             printf("audio stream rate %d channels %d\n",
  141.             aCodecCtx->sample_rate, aCodecCtx->channels);
  142.  
  143.             whdr.riff_id = 0x46464952;
  144.             whdr.riff_format = 0x45564157;
  145.             whdr.wFormatTag = 0x01;
  146.             whdr.nSamplesPerSec = aCodecCtx->sample_rate;
  147.             whdr.nChannels = aCodecCtx->channels;
  148.             whdr.wBitsPerSample = 16;
  149.  
  150.             sample_rate = aCodecCtx->sample_rate;
  151.  
  152.             fmt = test_wav(&whdr);
  153.  
  154.             if( init_audio(fmt) )
  155.             {
  156.                 decoder_buffer = (uint8_t*)av_mallocz(AVCODEC_MAX_AUDIO_FRAME_SIZE*2+64);
  157.                 if( decoder_buffer != NULL )
  158.                 {
  159.                     astream.lock   = 0;
  160.                     astream.count  = 0;
  161.                     astream.buffer = (char *)av_mallocz(AVCODEC_MAX_AUDIO_FRAME_SIZE*8);
  162.                     if( astream.buffer != NULL )
  163.                         have_sound = 1;
  164.                     else
  165.                         av_free(decoder_buffer);
  166.                 }
  167.                 if( have_sound == 0)
  168.                 {
  169.                         printf("Not enough memory for audio buffers\n");
  170.                 }
  171.             }
  172.         }
  173.         else printf("Cannot open audio codec\n\r");
  174.     }
  175.     else printf("Unsupported audio codec!\n");
  176.  
  177.     if( !init_video(pCodecCtx))
  178.         return 0;
  179.  
  180. //    __asm__ __volatile__("int3");
  181.  
  182.     decoder();
  183.  
  184.   // Free the YUV frame
  185.     av_free(pFrame);
  186.  
  187.  
  188. //__asm__ __volatile__("int3");
  189.  
  190.   // Close the codec
  191.  // avcodec_close(pCodecCtx);
  192.  
  193.   // Close the video file
  194.  // av_close_input_file(pFormatCtx);
  195.  
  196. //__asm__ __volatile__("int3");
  197.  
  198.     return 0;
  199. }
  200.  
  201. void decoder()
  202. {
  203.     int       eof = 0;
  204.     AVPacket  packet;
  205.     int       ret;
  206.  
  207.     while( status != 0 && !eof)
  208.     {
  209.         int err;
  210.  
  211. //        __asm__ __volatile__("int3");
  212.  
  213.         if(q_video.size+q_audio.size < 12*1024*1024)
  214.         {
  215.             err = av_read_frame(pFormatCtx, &packet);
  216.             if( err < 0)
  217.             {
  218.                 eof = 1;
  219.                 if (err != AVERROR_EOF)
  220.                     printf("av_read_frame: error %x\n", err);
  221.                 continue;
  222.             }
  223.             if(packet.stream_index==videoStream)
  224.             {
  225.                 put_packet(&q_video, &packet);
  226.             }
  227.             else if( (packet.stream_index == audioStream) &&
  228.                  (have_sound != 0) )
  229.             {
  230.                 put_packet(&q_audio, &packet);
  231.             }
  232.             else
  233.             {
  234.                 av_free_packet(&packet);
  235.             };
  236.             decode_video(pCodecCtx, &q_video);
  237.             decode_audio(aCodecCtx, &q_audio);
  238.             continue;
  239.         };
  240.         decode_video(pCodecCtx, &q_video);
  241.         decode_audio(aCodecCtx, &q_audio);
  242.         delay(1);
  243.     };
  244.  
  245.     ret = 1;
  246.  
  247.     while( (status != 0) && ret)
  248.     {
  249.         ret =  decode_video(pCodecCtx, &q_video);
  250.         ret |= decode_audio(aCodecCtx, &q_audio);
  251.         delay(1);
  252.     };
  253.     delay(50);
  254.     status = 0;
  255.     printf("status = 0\n");
  256.     delay(300);
  257. };
  258.  
  259.