Subversion Repositories Kolibri OS

Rev

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