* Audio decoding. */
| 114 | * Audio decoding. |
| 115 | */ |
| 116 | static void audio_decode_example(const char *outfilename, const char *filename) |
| 117 | { |
| 118 | AVCodec *codec; |
| 119 | AVCodecContext *c= NULL; |
| 120 | int out_size, len; |
| 121 | FILE *f, *outfile; |
| 122 | uint8_t *outbuf; |
| 123 | uint8_t inbuf[AUDIO_INBUF_SIZE + FF_INPUT_BUFFER_PADDING_SIZE]; |
| 124 | AVPacket avpkt; |
| 125 | |
| 126 | av_init_packet(&avpkt); |
| 127 | |
| 128 | printf("Audio decoding\n"); |
| 129 | |
| 130 | /* find the mpeg audio decoder */ |
| 131 | codec = avcodec_find_decoder(CODEC_ID_MP2); |
| 132 | if (!codec) { |
| 133 | fprintf(stderr, "codec not found\n"); |
| 134 | exit(1); |
| 135 | } |
| 136 | |
| 137 | c= avcodec_alloc_context(); |
| 138 | |
| 139 | /* open it */ |
| 140 | if (avcodec_open(c, codec) < 0) { |
| 141 | fprintf(stderr, "could not open codec\n"); |
| 142 | exit(1); |
| 143 | } |
| 144 | |
| 145 | outbuf = malloc(AVCODEC_MAX_AUDIO_FRAME_SIZE); |
| 146 | |
| 147 | f = fopen(filename, "rb"); |
| 148 | if (!f) { |
| 149 | fprintf(stderr, "could not open %s\n", filename); |
| 150 | exit(1); |
| 151 | } |
| 152 | outfile = fopen(outfilename, "wb"); |
| 153 | if (!outfile) { |
| 154 | av_free(c); |
| 155 | exit(1); |
| 156 | } |
| 157 | |
| 158 | /* decode until eof */ |
| 159 | avpkt.data = inbuf; |
| 160 | avpkt.size = fread(inbuf, 1, AUDIO_INBUF_SIZE, f); |
| 161 | |
| 162 | while (avpkt.size > 0) { |
| 163 | out_size = AVCODEC_MAX_AUDIO_FRAME_SIZE; |
| 164 | len = avcodec_decode_audio3(c, (short *)outbuf, &out_size, &avpkt); |
| 165 | if (len < 0) { |
| 166 | fprintf(stderr, "Error while decoding\n"); |
| 167 | exit(1); |
| 168 | } |
| 169 | if (out_size > 0) { |
| 170 | /* if a frame has been decoded, output it */ |
| 171 | fwrite(outbuf, 1, out_size, outfile); |
| 172 | } |
| 173 | avpkt.size -= len; |
no test coverage detected