Subversion Repositories Kolibri OS

Rev

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