Advertisement
eyjohn

libav open/read

Dec 6th, 2011
428
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.27 KB | None | 0 0
  1.  
  2.     if (format != NULL){
  3.         fmt = av_find_input_format(format);
  4.         res = avformat_open_input(&oc, url, fmt , NULL);
  5.     } else {
  6.         res = avformat_open_input(&oc, url, NULL , NULL);
  7.     }
  8.  
  9.  
  10.     if (res < 0) {
  11.         fprintf(stderr,"[LIBAV] Unable to open input file/url: %s\n", url);
  12.         return res;
  13.     }
  14.  
  15.     res = avformat_find_stream_info(oc, NULL);
  16.  
  17.     if (res < 0) {
  18.         fprintf(stderr,"[LIBAV] Unable to read stream info for file/url: %s\n", url);
  19.         libavClose();
  20.         return res;
  21.     }
  22.  
  23.  
  24.     res = av_find_best_stream(oc, AVMEDIA_TYPE_VIDEO, -1, -1, &video_decoder, 0);
  25.  
  26.     if (res < 0){
  27.         fprintf(stderr,"[LIBAV] No video stream found\n");
  28.         libavClose();
  29.         return -1;
  30.     }
  31.  
  32.  
  33.     video_st_index = res;
  34.     video_st = oc->streams[video_st_index];
  35.     video_codec = video_st->codec;
  36.  
  37.     if((res = avcodec_open2(video_codec, video_decoder, NULL))<0) {
  38.         char error[1024];
  39.         av_strerror(res, error, sizeof(error));
  40.  
  41.         fprintf(stderr,"[LIBAV] Could not open video decoder: %s\n", error);
  42.         libavClose();
  43.         return -1;
  44.     }
  45.  
  46.     // Fetch Audio stream and Decoder
  47.     res = av_find_best_stream(oc, AVMEDIA_TYPE_AUDIO, -1, -1, &audio_decoder, 0);
  48.  
  49.     if (res < 0){
  50.         fprintf(stderr,"[LIBAV] No audio stream found\n");
  51.         libavClose();
  52.         return -1;
  53.     }
  54.  
  55.     audio_st_index = res;
  56.     audio_st = oc->streams[audio_st_index];
  57.     audio_codec = audio_st->codec;
  58.  
  59.  
  60.     if((res = avcodec_open2(audio_codec, audio_decoder, NULL))<0) {
  61.         char error[1024];
  62.         av_strerror(res, error, sizeof(error));
  63.  
  64.         fprintf(stderr,"[LIBAV] Could not open audio decoder: %s\n", error);
  65.         libavClose();
  66.         return -1;
  67.     }
  68.  
  69.     // Dump source information
  70.     av_dump_format(oc,0,url,0);
  71.  
  72.    
  73.     // Setup audio/video
  74.     video_buf = avcodec_alloc_frame();
  75.     audio_buf = (u_int8_t *) av_malloc(AVCODEC_MAX_AUDIO_FRAME_SIZE);
  76.  
  77.     while ((res = av_read_frame(oc, &packet))>=0 ) {
  78.         orig = packet;
  79.         if (packet.stream_index == audio_st_index) {
  80.  
  81.             // Keep reading audio samples from the packet (may contain multiple)
  82.             while (packet.size > 0) {
  83.                 int chunk = AVCODEC_MAX_AUDIO_FRAME_SIZE;
  84.                 int in_used = avcodec_decode_audio3(audio_codec, (int16_t *) ((u_int8_t *) audio_buf + (*audio_samples) * av_get_bytes_per_sample( audio_st->codec->sample_fmt) * audio_st->codec->channels ) , &chunk, &packet);
  85.  
  86.                 if (in_used < 0) {
  87.                     //printf("Invalid Audio Frame\n");
  88.                     // Decoding error -- skip this frame.
  89.                     break;
  90.                 }
  91.  
  92.                 audio_samples += (chunk / av_get_bytes_per_sample( audio_st->codec->sample_fmt)) /  audio_st->codec->channels;
  93.  
  94.                 packet.data += in_used;
  95.                 packet.size -= in_used;
  96.  
  97.             }
  98.  
  99.             // If audio_samples were written, then update pointer
  100.             if (audio_samples > 0) {
  101.                 // audio_buf stores audio_samples number of samples
  102.                 sampleCount += audio_samples;
  103.             }
  104.  
  105.         } else if (packet.stream_index == video_st_index) {
  106.        
  107.  
  108.             if (packet.size > 0) {
  109.  
  110.                 frameFinished = 0;
  111.  
  112.                 int in_used = avcodec_decode_video2(video_codec,video_buf, &frameFinished, &packet);
  113.  
  114.                 if (in_used < 0) {
  115.                     //printf("Invalid Video Frame\n");
  116.                     // Decoding error -- skip this frame.
  117.  
  118.                 } else {
  119.  
  120.                     if(frameFinished) {
  121.  
  122.                         frameCount++;
  123.  
  124.                         // video_frame stores the current frame
  125.  
  126.                     }
  127.                     packet.data += in_used;
  128.                     packet.size -= in_used;
  129.                 }
  130.  
  131.             }
  132.  
  133.         }
  134.  
  135.         av_free_packet(&orig); // free the packet
  136.     }
  137.  
  138.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement