Subversion Repositories Kolibri OS

Rev

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