Advertisement
RyanMarcus

decoder.cpp

Jan 18th, 2016
181
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 4.21 KB | None | 0 0
  1. /*
  2. Program output:
  3.  
  4. [mov,mp4,m4a,3gp,3g2,mj2 @ 0x7fa75080c200] stream 0, timescale not set
  5. [mjpeg @ 0x7fa751000c00] Changeing bps to 8
  6. Input #0, mov,mp4,m4a,3gp,3g2,mj2, from 'test.m4a':
  7.   Metadata:
  8.     major_brand     : M4A
  9.     minor_version   : 0
  10.     compatible_brands: M4A mp42isom
  11.     creation_time   : 2029-10-02 09:03:14
  12.     title           : Heartsigh
  13.     artist          : Purity Ring
  14.     album_artist    : Purity Ring
  15.     album           : Another Eternity
  16.     genre           : Synth-pop
  17.     composer        : Corin Roddick
  18.     track           : 1/10
  19.     disc            : 1/1
  20.     date            : 2015
  21.     encoder         : X Lossless Decoder 20141129a, QuickTime 7.7.3
  22.   Duration: 00:03:19.14, start: 0.000000, bitrate: 2962 kb/s
  23.     Stream #0:0(eng): Audio: alac (alac / 0x63616C61), 96000 Hz, stereo, s32p (24 bit), 2955 kb/s (default)
  24.     Metadata:
  25.       creation_time   : 2029-10-02 09:03:14
  26.     Stream #0:1: Video: mjpeg, yuvj420p(pc, bt470bg/unknown/unknown), 1000x1000 [SAR 72:72 DAR 1:1], 90k tbr, 90k tbn, 90k tbc
  27. Using codec: ALAC (Apple Lossless Audio Codec) for stream 0
  28. [alac @ 0x7fa750808a00] extradata is too small
  29. could not open codec
  30. error: Invalid data found when processing input
  31.  
  32. */
  33.  
  34.  
  35. #include <stdio.h>
  36. extern "C" {
  37. #include <libavcodec/avcodec.h>
  38. #include <libavformat/avformat.h>
  39. }
  40. //#include <ffmpeg/swscale.h>
  41.  
  42. void printError(int err) {
  43.     char buf[300];
  44.     if (av_strerror(err, buf, 300) < 0) {
  45.         printf("unknown error code: %d\n", err);
  46.     } else {
  47.         printf("error: %s\n", buf);
  48.     }
  49. }
  50.  
  51.  
  52.  
  53. int main(int argc, char** argv) {
  54.  
  55.     // register all the needed ffmpeg formats and codecs...
  56.     av_register_all();
  57.  
  58.     AVFormatContext* pFormatCtx = NULL;
  59.  
  60.     if (avformat_open_input(&pFormatCtx, "test.m4a", NULL, NULL) != 0) {
  61.         printf("error: couldn't open file.\n");
  62.         return -1;
  63.     }
  64.  
  65.     if (avformat_find_stream_info(pFormatCtx, NULL) != 0) {
  66.         printf("error: couldn't find stream information\n");
  67.         return -1;
  68.     }
  69.  
  70.     // dump debugging info about the stream
  71.     av_dump_format(pFormatCtx, 0, "test.m4a", 0);
  72.    
  73.     // figure out which stream is the "best" stream
  74.     AVCodec* codec;
  75.     int firstAudio = av_find_best_stream(pFormatCtx, AVMEDIA_TYPE_AUDIO, -1, -1, &codec, 0);
  76.  
  77.     if (firstAudio == AVERROR_STREAM_NOT_FOUND) {
  78.         printf("could not find an audio stream\n");
  79.         return -1;
  80.     } else if (firstAudio == AVERROR_DECODER_NOT_FOUND) {
  81.         printf("found an audio stream, but no decoder\n");
  82.         return -1;
  83.     } else if (firstAudio < 0) {
  84.         printf("error finding audio stream and codec\n");
  85.         return -1;
  86.     }
  87.    
  88.    
  89.     printf("Using codec: %s for stream %d\n", codec->long_name, firstAudio);
  90.  
  91.     AVCodecContext* pCodecCtx = avcodec_alloc_context3(codec);
  92.  
  93.  
  94.    
  95.     if (pCodecCtx == NULL) {
  96.         printf("could not allocate a codec context\n");
  97.         return -1;
  98.     }
  99.    
  100.     int err;
  101.     if ((err = avcodec_open2(pCodecCtx, codec, NULL)) != 0) {
  102.         printf("could not open codec\n");
  103.         printError(err);
  104.         return -1;
  105.     }
  106.  
  107.  
  108.  
  109.     /*
  110.          PROGRAM DIES ABOVE
  111.      */
  112.  
  113.  
  114.  
  115.  
  116.  
  117.  
  118.    
  119.     int bytesPerSample = av_get_bytes_per_sample(pCodecCtx->sample_fmt);
  120.     if (bytesPerSample <= 0) {
  121.         printf("could not determine proper number of bytes per sample\n");
  122.         return -1;
  123.     }
  124.     printf("Bytes per sample: %d\n", bytesPerSample);
  125.    
  126.     AVFrame *pFrame = av_frame_alloc();
  127.     AVPacket packet;
  128.     av_init_packet(&packet);
  129.  
  130.     int bufferSize = 20480 + FF_INPUT_BUFFER_PADDING_SIZE;
  131.     uint8_t buffer[bufferSize];
  132.     packet.data = buffer;
  133.     packet.size = bufferSize;
  134.  
  135.    
  136.     while (av_read_frame(pFormatCtx, &packet) >= 0) {
  137.         if (packet.stream_index != firstAudio)
  138.             continue;
  139.  
  140.         // decode a frame
  141.         int totalRead = 0;
  142.         while (totalRead <= packet.size) {
  143.             int gotFrame, read = 0;
  144.             read += avcodec_decode_audio4(pCodecCtx, pFrame, &gotFrame, &packet);
  145.  
  146.             if (read < 0) {
  147.                 printf("error during decoding\n");
  148.                 printError(read);
  149.                 return -1;
  150.             }
  151.            
  152.             printf("%d\n", gotFrame);
  153.             if (gotFrame == 0)
  154.                 continue;
  155.             totalRead += read;
  156.             printf("%d / %d\n", totalRead, packet.size);
  157.            
  158.             for (int i = 0; i < pFrame->nb_samples; i++) {
  159.                 for (int ch = 0; ch < pCodecCtx->channels; ch++) {
  160.                     //print the first byte of each sample...
  161.                     printf("%d\n", pFrame->data[ch][i]);
  162.                 }
  163.             }
  164.         }
  165.     }
  166.  
  167.  
  168.  
  169.  
  170.  
  171.    
  172.  
  173.    
  174.  
  175.  
  176.  
  177. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement