Subversion Repositories Kolibri OS

Rev

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