Subversion Repositories Kolibri OS

Rev

Rev 6136 | Rev 6301 | Go to most recent revision | Blame | Compare with Previous | Last modification | View Log | 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 <kos32sys.h>
  14. #include "winlib/winlib.h"
  15.  
  16. #include "sound.h"
  17. #include "fplay.h"
  18.  
  19. volatile enum player_state player_state  = STOP;
  20. volatile enum player_state decoder_state = PREPARE;
  21. volatile enum player_state sound_state   = STOP;
  22.  
  23. uint32_t win_width, win_height;
  24.  
  25. uint8_t  *decoder_buffer;
  26. extern int resampler_size;
  27. extern int sample_rate;
  28.  
  29. int64_t  rewind_pos;
  30.  
  31. int64_t stream_duration;
  32.  
  33. int threads_running = DECODER_THREAD;
  34.  
  35.  
  36. int main( int argc, char *argv[])
  37. {
  38.     static vst_t vst;
  39.     int i, ret;
  40.     char *file_name, *dot;
  41.  
  42.     if(argc < 2)
  43.     {
  44.         vst.input_file = get_moviefile();
  45.         if(vst.input_file == NULL)
  46.         {
  47.             printf("Please provide a movie file\n");
  48.             return -1;
  49.         }
  50.     }
  51.     else vst.input_file = argv[1];
  52.  
  53.     /* register all codecs, demux and protocols */
  54.  
  55.     av_log_set_level(AV_LOG_FATAL);
  56.  
  57.     avcodec_register_all();
  58.     avdevice_register_all();
  59.     av_register_all();
  60.  
  61.     if( avformat_open_input(&vst.fCtx, vst.input_file, NULL, NULL) < 0)
  62.     {
  63.         printf("Cannot open file %s\n\r", vst.input_file);
  64.         return -1; // Couldn't open file
  65.     };
  66.  
  67.     vst.fCtx->flags |= AVFMT_FLAG_GENPTS;
  68.  
  69.     if(avformat_find_stream_info(vst.fCtx, NULL) < 0)
  70.     {
  71.         printf("Cannot find streams\n\r");
  72.         return -1;
  73.     };
  74.  
  75.     file_name = strrchr(vst.input_file,'/')+1;
  76.     dot = strrchr(file_name,'.');
  77.     if(dot)
  78.     {
  79.         vst.input_name = malloc(dot-file_name+1);
  80.         memcpy(vst.input_name, file_name, dot-file_name);
  81.         vst.input_name[dot-file_name] = 0;
  82.     }
  83.     else vst.input_name = file_name;
  84.  
  85.     stream_duration = vst.fCtx->duration;
  86.  
  87.     vst.vStream = -1;
  88.     vst.aStream = -1;
  89.  
  90.     for(i=0; i < vst.fCtx->nb_streams; i++)
  91.     {
  92.         if(vst.fCtx->streams[i]->codec->codec_type==AVMEDIA_TYPE_VIDEO
  93.             && vst.vStream < 0)
  94.         {
  95.             vst.vStream = i;
  96.             vst.video_time_base = vst.fCtx->streams[i]->time_base;
  97.             if(stream_duration == 0)
  98.                stream_duration = vst.fCtx->streams[i]->duration;
  99.         }
  100.  
  101.         if(vst.fCtx->streams[i]->codec->codec_type==AVMEDIA_TYPE_AUDIO &&
  102.            vst.aStream < 0)
  103.         {
  104.             vst.aStream = i;
  105.             if(stream_duration == 0)
  106.                stream_duration = vst.fCtx->streams[i]->duration;
  107.         }
  108.     }
  109.  
  110.     if(vst.vStream==-1)
  111.     {
  112.         printf("Video stream not detected\n\r");
  113.         return -1; // Didn't find a video stream
  114.     };
  115.  
  116.   //   __asm__ __volatile__("int3");
  117.  
  118.     INIT_LIST_HEAD(&vst.input_list);
  119.     INIT_LIST_HEAD(&vst.output_list);
  120.     mutex_init(&vst.q_video.lock);
  121.     mutex_init(&vst.q_audio.lock);
  122.     mutex_init(&vst.gpu_lock);
  123.     mutex_init(&vst.decoder_lock);
  124.     mutex_init(&vst.input_lock);
  125.     mutex_init(&vst.output_lock);
  126.  
  127.     vst.vCtx = vst.fCtx->streams[vst.vStream]->codec;
  128.     vst.aCtx = vst.fCtx->streams[vst.aStream]->codec;
  129.  
  130.     if(init_video_decoder(&vst) != 0 )
  131.         return -1;
  132.  
  133.     vst.aCtx->request_channel_layout = AV_CH_LAYOUT_STEREO;
  134.     vst.aCodec = avcodec_find_decoder(vst.aCtx->codec_id);
  135.     if(vst.aCodec)
  136.     {
  137.         if(avcodec_open2(vst.aCtx, vst.aCodec, NULL) >= 0 )
  138.         {
  139.             WAVEHEADER       whdr;
  140.             int fmt;
  141.             int channels;
  142.  
  143.             printf("audio stream rate %d channels %d format %d\n",
  144.             vst.aCtx->sample_rate, vst.aCtx->channels, vst.aCtx->sample_fmt );
  145.             whdr.riff_id = 0x46464952;
  146.             whdr.riff_format = 0x45564157;
  147.             whdr.wFormatTag = 0x01;
  148.             whdr.nSamplesPerSec = vst.aCtx->sample_rate;
  149.             whdr.nChannels = 2;
  150.             whdr.wBitsPerSample = 16;
  151.  
  152.             sample_rate = vst.aCtx->sample_rate;
  153.  
  154.             fmt = test_wav(&whdr);
  155.  
  156.             if( init_audio(fmt) )
  157.             {
  158.                 decoder_buffer = (uint8_t*)av_mallocz(192000*2+64);
  159.                 if( decoder_buffer != NULL )
  160.                 {
  161.                     mutex_init(&astream.lock);
  162.                     astream.count  = 0;
  163.                     astream.buffer = (char *)av_mallocz(192000*3);
  164.                     if( astream.buffer != NULL )
  165.                         vst.has_sound = 1;
  166.                     else
  167.                         av_free(decoder_buffer);
  168.                 }
  169.                 if( vst.has_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.     mutex_lock(&vst.decoder_lock);
  180.     create_thread(video_thread, &vst, 1024*1024);
  181.     if(mutex_lock_timeout(&vst.decoder_lock, 3000) == 0)
  182.         return -1;
  183.  
  184.     decoder(&vst);
  185.  
  186.  
  187. //__asm__ __volatile__("int3");
  188.  
  189.     while( threads_running &
  190.            (AUDIO_THREAD | VIDEO_THREAD))
  191.            delay(1);
  192.  
  193.     if(astream.lock.handle)
  194.         mutex_destroy(&astream.lock);
  195.  
  196.     fini_video_decoder(&vst);
  197.     mutex_destroy(&vst.q_video.lock);
  198.     mutex_destroy(&vst.q_audio.lock);
  199.     mutex_destroy(&vst.decoder_lock);
  200.  
  201.     return 0;
  202. }
  203.  
  204.  
  205. static int load_frame(vst_t *vst)
  206. {
  207.     AVPacket  packet;
  208.     int err;
  209.  
  210.     err = av_read_frame(vst->fCtx, &packet);
  211.     if( err == 0)
  212.     {
  213.         if(packet.stream_index == vst->vStream)
  214.             put_packet(&vst->q_video, &packet);
  215.         else if( (packet.stream_index == vst->aStream) &&
  216.                   (vst->has_sound != 0) )
  217.         {
  218.             put_packet(&vst->q_audio, &packet);
  219.             if(vst->audio_timer_valid == 0 &&
  220.                packet.pts != AV_NOPTS_VALUE )
  221.             {
  222.                 vst->audio_timer_base = get_audio_base(vst) * packet.pts;
  223.                 vst->audio_timer_valid = 1;
  224.             };
  225.         }
  226.         else av_free_packet(&packet);
  227.     }
  228.     else if (err != AVERROR_EOF)
  229.         printf("av_read_frame: error %x\n", err);
  230.  
  231.     return err;
  232. }
  233.  
  234.  
  235.  
  236. static int fill_queue(vst_t* vst)
  237. {
  238.     int err = 0;
  239.     AVPacket  packet;
  240.  
  241.     while( (vst->q_video.size < 4*1024*1024) && !err )
  242.         err = load_frame(vst);
  243.  
  244.     return err;
  245. };
  246.  
  247.  
  248. static void flush_all(vst_t* vst)
  249. {
  250.     AVPacket  packet;
  251.  
  252.     avcodec_flush_buffers(vst->vCtx);
  253.     avcodec_flush_buffers(vst->aCtx);
  254.     while( get_packet(&vst->q_video, &packet) != 0)
  255.         av_free_packet(&packet);
  256.  
  257.     while( get_packet(&vst->q_audio, &packet)!= 0)
  258.         av_free_packet(&packet);
  259.  
  260.     flush_video(vst);
  261.  
  262.     astream.count = 0;
  263. };
  264.  
  265. void decoder(vst_t* vst)
  266. {
  267.     int       eof;
  268.     AVPacket  packet;
  269.     int       ret, vret, aret;
  270.  
  271.     int64_t   min_pos, max_pos;
  272.  
  273. //    av_log_set_level(AV_LOG_DEBUG);
  274.  
  275.     while( player_state != CLOSED )
  276.     {
  277.         int err;
  278.  
  279.         switch(decoder_state)
  280.         {
  281.             case PREPARE:
  282.                 eof = fill_queue(vst);
  283.  
  284.                 do
  285.                 {
  286.                     if( (vst->q_video.size < 4*1024*1024) &&
  287.                         (eof == 0) )
  288.                     {
  289.                         eof = load_frame(vst);
  290.                     }
  291.                     decode_video(vst);
  292.                     ret = decode_audio(vst->aCtx, &vst->q_audio);
  293.                 }while(astream.count < resampler_size*2 && ret == 1);
  294.  
  295.                 sound_state   = PREPARE;
  296.                 decoder_state = PLAY;
  297.                 player_state  = PLAY;
  298.  
  299.             case PLAY:
  300.                 if( (vst->q_video.size < 4*1024*1024) &&
  301.                     (eof == 0) )
  302.                 {
  303.                     eof = load_frame(vst);
  304.                 }
  305.                 vret = decode_video(vst);
  306.                 aret = decode_audio(vst->aCtx, &vst->q_audio);
  307.                 ret = vret | aret;
  308.  
  309.                 if( eof && !ret)
  310.                 {
  311.                     decoder_state = STOP;
  312.                     continue;
  313.                 };
  314.  
  315.                 if( (vret & aret) == -1)
  316.                 {
  317.                     if( (vst->q_video.size < 4*1024*1024) &&
  318.                         (eof == 0) )
  319.                     {
  320.                         eof = load_frame(vst);
  321.                         yield();
  322.                         continue;
  323.                     };
  324.                     delay(1);
  325.                     continue;
  326.                 }
  327.                 yield();
  328.                 continue;
  329.  
  330.             case STOP:
  331.                 delay(1);
  332.                 continue;
  333.  
  334.  
  335.             case PLAY_2_STOP:
  336.                 while(sound_state != STOP)
  337.                     delay(1);
  338.  
  339.                 flush_all(vst);
  340.  
  341.                 if (vst->fCtx->start_time != AV_NOPTS_VALUE)
  342.                     rewind_pos = vst->fCtx->start_time;
  343.                 else
  344.                     rewind_pos = 0;
  345.  
  346.                 ret = avformat_seek_file(vst->fCtx, -1, INT64_MIN,
  347.                                          rewind_pos, INT64_MAX, 0);
  348.  
  349.                 decoder_state = STOP;
  350.                 break;
  351.  
  352.             case REWIND:
  353.                 while(sound_state != STOP)
  354.                     yield();
  355.  
  356.                 flush_all(vst);
  357.                 int opts = 0;
  358.                 if(rewind_pos < 0)
  359.                 {
  360.                     rewind_pos = -rewind_pos;
  361.                     opts = AVSEEK_FLAG_BACKWARD;
  362.                 };
  363.  
  364.                 if (vst->fCtx->start_time != AV_NOPTS_VALUE)
  365.                     rewind_pos += vst->fCtx->start_time;
  366.  
  367. //                printf("rewind %8"PRId64"\n", rewind_pos);
  368.                 min_pos = rewind_pos - 1000000;
  369.                 max_pos = rewind_pos + 1000000;
  370.  
  371.                 ret = avformat_seek_file(vst->fCtx, -1, INT64_MIN,
  372.                                          rewind_pos, INT64_MAX, 0);
  373.                 if (ret < 0)
  374.                 {
  375.                     printf("could not seek to position %f\n",
  376.                             (double)rewind_pos / AV_TIME_BASE);
  377.                 }
  378.                 decoder_state = PREPARE;
  379.                 break;
  380.         }
  381.     };
  382. };
  383.  
  384.