Subversion Repositories Kolibri OS

Rev

Rev 6118 | Rev 6133 | 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. #ifdef HAVE_VAAPI
  20. int va_check_codec_support(enum AVCodecID id);
  21. #endif
  22.  
  23. volatile enum player_state player_state  = STOP;
  24. volatile enum player_state decoder_state = PREPARE;
  25. volatile enum player_state sound_state   = STOP;
  26.  
  27. uint32_t win_width, win_height;
  28.  
  29.  
  30. AVFrame *pFrame;
  31.  
  32. int      have_sound = 0;
  33.  
  34. uint8_t  *decoder_buffer;
  35. extern int resampler_size;
  36.  
  37. extern int sample_rate;
  38. char *movie_file;
  39.  
  40. void flush_video(vst_t* vst);
  41.  
  42.  
  43. int64_t  rewind_pos;
  44.  
  45. int64_t stream_duration;
  46.  
  47. int threads_running = DECODER_THREAD;
  48.  
  49. extern double audio_base;
  50.  
  51.  
  52. double get_audio_base(vst_t* vst)
  53. {
  54.     return (double)av_q2d(vst->fCtx->streams[vst->aStream]->time_base)*1000;
  55. };
  56.  
  57.  
  58. int main( int argc, char *argv[])
  59. {
  60.     static vst_t vst;
  61.     int i, ret;
  62.     char *file_name, *dot;
  63.  
  64.     if(argc < 2)
  65.     {
  66.         movie_file = get_moviefile();
  67.         if(movie_file == NULL)
  68.         {
  69.             printf("Please provide a movie file\n");
  70.             return -1;
  71.         }
  72.     }
  73.     else movie_file = argv[1];
  74.  
  75.     /* register all codecs, demux and protocols */
  76.  
  77.     av_log_set_level(AV_LOG_FATAL);
  78.  
  79.     avcodec_register_all();
  80.     avdevice_register_all();
  81.     av_register_all();
  82.  
  83.     if( avformat_open_input(&vst.fCtx, movie_file, NULL, NULL) < 0)
  84.     {
  85.         printf("Cannot open file %s\n\r", movie_file);
  86.         return -1; // Couldn't open file
  87.     };
  88.  
  89.     vst.fCtx->flags |= AVFMT_FLAG_GENPTS;
  90.  
  91.   // Retrieve stream information
  92.     if(avformat_find_stream_info(vst.fCtx, NULL) < 0)
  93.     {
  94.         printf("Cannot find streams\n\r");
  95.         return -1;
  96.     };
  97.  
  98.     file_name = strrchr(movie_file,'/')+1;
  99.     dot = strrchr(file_name,'.');
  100.     if(dot)
  101.     {
  102.         movie_file = malloc(dot-file_name+1);
  103.         memcpy(movie_file, file_name, dot-file_name);
  104.         movie_file[dot-file_name] = 0;
  105.     }
  106.     else movie_file = file_name;
  107.  
  108.     stream_duration = vst.fCtx->duration;
  109.  
  110.    // Find the first video stream
  111.     vst.vStream = -1;
  112.     vst.aStream = -1;
  113.  
  114.     for(i=0; i < vst.fCtx->nb_streams; i++)
  115.     {
  116.         if(vst.fCtx->streams[i]->codec->codec_type==AVMEDIA_TYPE_VIDEO
  117.             && vst.vStream < 0)
  118.         {
  119.             vst.vStream = i;
  120.             video_time_base = vst.fCtx->streams[i]->time_base;
  121.             if(stream_duration == 0)
  122.                stream_duration = vst.fCtx->streams[i]->duration;
  123.         }
  124.  
  125.         if(vst.fCtx->streams[i]->codec->codec_type==AVMEDIA_TYPE_AUDIO &&
  126.            vst.aStream < 0)
  127.         {
  128.             vst.aStream = i;
  129.             if(stream_duration == 0)
  130.                stream_duration = vst.fCtx->streams[i]->duration;
  131.         }
  132.     }
  133.  
  134.     if(vst.vStream==-1)
  135.     {
  136.         printf("Video stream not detected\n\r");
  137.         return -1; // Didn't find a video stream
  138.     };
  139.  
  140.   //   __asm__ __volatile__("int3");
  141.  
  142.     // Get a pointer to the codec context for the video stream
  143.     vst.vCtx = vst.fCtx->streams[vst.vStream]->codec;
  144.     vst.aCtx = vst.fCtx->streams[vst.aStream]->codec;
  145.  
  146.     vst.vCodec = avcodec_find_decoder(vst.vCtx->codec_id);
  147.     printf("codec id %x name %s\n",vst.vCtx->codec_id, vst.vCodec->name);
  148.     printf("ctx->pix_fmt %d\n", vst.vCtx->pix_fmt);
  149.  
  150.     if(vst.vCodec == NULL)
  151.     {
  152.         printf("Unsupported codec with id %d for input stream %d\n",
  153.         vst.vCtx->codec_id, vst.vStream);
  154.         return -1; // Codec not found
  155.     }
  156.  
  157.     if(fplay_init_context(&vst))
  158.         return -1;
  159.  
  160.     if(avcodec_open2(vst.vCtx, vst.vCodec, NULL) < 0)
  161.     {
  162.         printf("Error while opening codec for input stream %d\n",
  163.                 vst.vStream);
  164.         return -1; // Could not open codec
  165.     };
  166.  
  167.     printf("ctx->pix_fmt %d\n", vst.vCtx->pix_fmt);
  168.  
  169.     mutex_init(&vst.q_video.lock);
  170.     mutex_init(&vst.q_audio.lock);
  171.     mutex_init(&vst.gpu_lock);
  172.  
  173.     if (vst.aCtx->channels > 0)
  174.         vst.aCtx->request_channels = FFMIN(2, vst.aCtx->channels);
  175.     else
  176.         vst.aCtx->request_channels = 2;
  177.  
  178.     vst.aCodec = avcodec_find_decoder(vst.aCtx->codec_id);
  179.  
  180.     if(vst.aCodec)
  181.     {
  182.         if(avcodec_open2(vst.aCtx, vst.aCodec, NULL) >= 0 )
  183.         {
  184.             WAVEHEADER       whdr;
  185.             int fmt;
  186.             int channels;
  187.  
  188.             printf("audio stream rate %d channels %d format %d\n",
  189.             vst.aCtx->sample_rate, vst.aCtx->channels, vst.aCtx->sample_fmt );
  190.             whdr.riff_id = 0x46464952;
  191.             whdr.riff_format = 0x45564157;
  192.             whdr.wFormatTag = 0x01;
  193.             whdr.nSamplesPerSec = vst.aCtx->sample_rate;
  194.             whdr.nChannels = 2;
  195.             whdr.wBitsPerSample = 16;
  196.  
  197.             sample_rate = vst.aCtx->sample_rate;
  198.  
  199.             fmt = test_wav(&whdr);
  200.  
  201.             if( init_audio(fmt) )
  202.             {
  203.                 decoder_buffer = (uint8_t*)av_mallocz(192000*2+64);
  204.                 if( decoder_buffer != NULL )
  205.                 {
  206.                     mutex_init(&astream.lock);
  207.                     astream.count  = 0;
  208.                     astream.buffer = (char *)av_mallocz(192000*3);
  209.                     if( astream.buffer != NULL )
  210.                         have_sound = 1;
  211.                     else
  212.                         av_free(decoder_buffer);
  213.                 }
  214.                 if( have_sound == 0)
  215.                 {
  216.                         printf("Not enough memory for audio buffers\n");
  217.                 }
  218.             }
  219.         }
  220.         else printf("Cannot open audio codec\n\r");
  221.     }
  222.     else printf("Unsupported audio codec!\n");
  223.  
  224.     if(!init_video(&vst))
  225.         return 0;
  226.  
  227.     decoder(&vst);
  228.  
  229.   // Free the YUV frame
  230.     av_free(pFrame);
  231.  
  232.  
  233. //__asm__ __volatile__("int3");
  234.  
  235.     while( threads_running &
  236.            (AUDIO_THREAD | VIDEO_THREAD))
  237.            delay(1);
  238.  
  239.     if(astream.lock.handle)
  240.         mutex_destroy(&astream.lock);
  241.  
  242.     mutex_destroy(&vst.q_video.lock);
  243.     mutex_destroy(&vst.q_audio.lock);
  244.  
  245.     return 0;
  246. }
  247.  
  248.  
  249. static int load_frame(vst_t *vst)
  250. {
  251.     AVPacket  packet;
  252.     int err;
  253.  
  254.     err = av_read_frame(vst->fCtx, &packet);
  255.     if( err == 0)
  256.     {
  257.         if(packet.stream_index == vst->vStream)
  258.             put_packet(&vst->q_video, &packet);
  259.         else if( (packet.stream_index == vst->aStream) &&
  260.                   (have_sound != 0) )
  261.         {
  262.             put_packet(&vst->q_audio, &packet);
  263.             if(audio_base == -1.0)
  264.             {
  265.                 if (packet.pts != AV_NOPTS_VALUE)
  266.                     audio_base = get_audio_base(vst) * packet.pts;
  267. //                    printf("audio base %f\n", audio_base);
  268.             };
  269.         }
  270.         else av_free_packet(&packet);
  271.     }
  272.     else if (err != AVERROR_EOF)
  273.         printf("av_read_frame: error %x\n", err);
  274.  
  275.     return err;
  276. }
  277.  
  278.  
  279.  
  280. static int fill_queue(vst_t* vst)
  281. {
  282.     int err = 0;
  283.     AVPacket  packet;
  284.  
  285.     while( (vst->q_video.size < 4*1024*1024) && !err )
  286.         err = load_frame(vst);
  287.  
  288.     return err;
  289. };
  290.  
  291.  
  292. static void flush_all(vst_t* vst)
  293. {
  294.     AVPacket  packet;
  295.  
  296.     avcodec_flush_buffers(vst->vCtx);
  297.     avcodec_flush_buffers(vst->aCtx);
  298.     while( get_packet(&vst->q_video, &packet) != 0)
  299.         av_free_packet(&packet);
  300.  
  301.     while( get_packet(&vst->q_audio, &packet)!= 0)
  302.         av_free_packet(&packet);
  303.  
  304.     flush_video(vst);
  305.  
  306.     astream.count = 0;
  307. };
  308.  
  309. void decoder(vst_t* vst)
  310. {
  311.     int       eof;
  312.     AVPacket  packet;
  313.     int       ret, vret, aret;
  314.  
  315.     int64_t   min_pos, max_pos;
  316.  
  317. //    av_log_set_level(AV_LOG_DEBUG);
  318.  
  319.     while( player_state != CLOSED )
  320.     {
  321.         int err;
  322.  
  323.         switch(decoder_state)
  324.         {
  325.             case PREPARE:
  326.                 eof = fill_queue(vst);
  327.  
  328.                 do
  329.                 {
  330.                     if( (vst->q_video.size < 4*1024*1024) &&
  331.                         (eof == 0) )
  332.                     {
  333.                         eof = load_frame(vst);
  334.                     }
  335.                     decode_video(vst);
  336.                     ret = decode_audio(vst->aCtx, &vst->q_audio);
  337.                 }while(astream.count < resampler_size*2 &&
  338.                        ret == 1);
  339.  
  340.                 sound_state   = PREPARE;
  341.                 decoder_state = PLAY;
  342.                 player_state  = PLAY;
  343.  
  344.             case PLAY:
  345.                 if( (vst->q_video.size < 4*1024*1024) &&
  346.                     (eof == 0) )
  347.                 {
  348.                     eof = load_frame(vst);
  349.                 }
  350.                 vret = decode_video(vst);
  351.                 aret = decode_audio(vst->aCtx, &vst->q_audio);
  352.                 ret = vret | aret;
  353.  
  354.                 if( eof && !ret)
  355.                 {
  356.                     decoder_state = STOP;
  357.                     continue;
  358.                 };
  359.  
  360.                 if( (vret & aret) == -1)
  361.                 {
  362.                     if( (vst->q_video.size < 4*1024*1024) &&
  363.                         (eof == 0) )
  364.                     {
  365.                         eof = load_frame(vst);
  366.                         yield();
  367.                         continue;
  368.                     };
  369.                     delay(1);
  370.                     continue;
  371.                 }
  372.  
  373.                 yield();
  374.                 continue;
  375.  
  376.             case STOP:
  377.                 delay(1);
  378.                 continue;
  379.  
  380.  
  381.             case PLAY_2_STOP:
  382.                 while(sound_state != STOP)
  383.                     delay(1);
  384.  
  385.                 flush_all(vst);
  386.  
  387.                 if (vst->fCtx->start_time != AV_NOPTS_VALUE)
  388.                     rewind_pos = vst->fCtx->start_time;
  389.                 else
  390.                     rewind_pos = 0;
  391.  
  392.                 ret = avformat_seek_file(vst->fCtx, -1, INT64_MIN,
  393.                                          rewind_pos, INT64_MAX, 0);
  394.  
  395.                 decoder_state = STOP;
  396.                 break;
  397.  
  398.             case REWIND:
  399.                 while(sound_state != STOP)
  400.                     yield();
  401.  
  402.                 flush_all(vst);
  403.                 int opts = 0;
  404.                 if(rewind_pos < 0)
  405.                 {
  406.                     rewind_pos = -rewind_pos;
  407.                     opts = AVSEEK_FLAG_BACKWARD;
  408.                 };
  409.  
  410.                 if (vst->fCtx->start_time != AV_NOPTS_VALUE)
  411.                     rewind_pos += vst->fCtx->start_time;
  412.  
  413. //                printf("rewind %8"PRId64"\n", rewind_pos);
  414.                 min_pos = rewind_pos - 1000000;
  415.                 max_pos = rewind_pos + 1000000;
  416.  
  417.                 ret = avformat_seek_file(vst->fCtx, -1, INT64_MIN,
  418.                                          rewind_pos, INT64_MAX, 0);
  419.                 if (ret < 0)
  420.                 {
  421.                     printf("could not seek to position %f\n",
  422.                             (double)rewind_pos / AV_TIME_BASE);
  423.                 }
  424.                 decoder_state = PREPARE;
  425.                 break;
  426.         }
  427.     };
  428. };
  429.  
  430.