| 5 | #include "log.h" |
| 6 |
| 7 | AVPacket pkt; |
| 8 | int video_stream_idx = -1, audio_stream_idx = -1; |
| 9 | AVCodecContext *video_dec_ctx = NULL, *audio_dec_ctx = NULL; |
| 10 | AVFrame *frame = NULL; |
| 11 | AVStream *video_stream = NULL, *audio_stream = NULL; |
| 12 | AVFormatContext *fmt_ctx = NULL; |
| 13 | int64_t total_duration; |
| 14 | int64_t current_duration; |
| 15 | char *src = NULL; |
| 16 | char *dest = NULL; |
| 17 | int lastLog = 10; |
| 18 |
| 19 | void cleanup_in() { |
| 20 | if (video_dec_ctx) { |
| 21 | avcodec_close(video_dec_ctx); |
| 22 | video_dec_ctx = NULL; |
| 23 | } |
| 24 | if (audio_dec_ctx) { |
| 25 | avcodec_close(audio_dec_ctx); |
| 26 | audio_dec_ctx = NULL; |
| 27 | } |
| 28 | if (fmt_ctx) { |
| 29 | avformat_close_input(&fmt_ctx); |
| 30 | fmt_ctx = NULL; |
| 31 | } |
| 32 | if (frame) { |
| 33 | av_frame_free(&frame); |
| 34 | frame = NULL; |
| 35 | } |
| 36 | if (src) { |
| 37 | free(src); |
| 38 | src = NULL; |
| 39 | } |
| 40 | if (dest) { |
| 41 | free(dest); |
| 42 | dest = NULL; |
| 43 | } |
| 44 |
| 45 | total_duration = 0; |
| 46 | current_duration = 0; |
| 47 | video_stream_idx = -1; |
| 48 | audio_stream_idx = -1; |
| 49 | video_stream = NULL; |
| 50 | audio_stream = NULL; |
| 51 | lastLog = 10; |
| 52 | } |
| 53 |
| 54 | void onError() { |
| 55 | cleanup_in(); |
| 56 | cleanup_out(); |
| 57 | } |
| 58 |
| 59 | void onDone() { |
| 60 | LOGD("OK\n"); |
| 61 | cleanup_in(); |
| 62 | cleanup_out(); |
| 63 | } |
| 64 |
| 65 | void onProgress() { |
| 66 | float progress = (float)current_duration / (float)total_duration * 100; |
| 67 | if (progress > lastLog) { |
| 68 | lastLog += 10; |
| 69 | LOGD("progress %.2f\n", progress); |
| 70 | } |
| 71 | } |
| 72 |
| 73 | int open_codec_context(int *stream_idx, AVFormatContext *fmt_ctx, enum AVMediaType type) { |
| 74 | int ret; |
| 75 | AVStream *st; |
| 76 | AVCodecContext *dec_ctx = NULL; |
| 77 | AVCodec *dec = NULL; |
| 78 | AVDictionary *opts = NULL; |
| 79 |
| 80 | ret = av_find_best_stream(fmt_ctx, type, -1, -1, NULL, 0); |
| 81 | if (ret < 0) { |
| 82 | LOGD("Could not find %s stream in input file\n", av_get_media_type_string(type)); |
| 83 | return ret; |
| 84 | } else { |
| 85 | *stream_idx = ret; |
| 86 | st = fmt_ctx->streams[*stream_idx]; |
| 87 |
| 88 | dec_ctx = st->codec; |
| 89 | dec = avcodec_find_decoder(dec_ctx->codec_id); |
| 90 | if (!dec) { |
| 91 | LOGD("Failed to find %s codec\n", av_get_media_type_string(type)); |
| 92 | return ret; |
| 93 | } |
| 94 |
| 95 | av_dict_set(&opts, "refcounted_frames", "1", 0); |
| 96 | if ((ret = avcodec_open2(dec_ctx, dec, &opts)) < 0) { |
| 97 | LOGD("Failed to open %s codec\n", av_get_media_type_string(type)); |
| 98 | return ret; |
| 99 | } |
| 100 | } |
| 101 |
| 102 | return 0; |
| 103 | } |
| 104 |
| 105 | int decode_packet(int *got_frame, int cached) { |
| 106 | int ret = 0; |
| 107 | int decoded = pkt.size; |
| 108 |
| 109 | *got_frame = 0; |
| 110 |
| 111 | if (pkt.stream_index == video_stream_idx) { |
| 112 | ret = avcodec_decode_video2(video_dec_ctx, frame, got_frame, &pkt); |
| 113 | if (ret < 0) { |
| 114 | LOGD("Error decoding video frame\n"); |
| 115 | return ret; |
| 116 | } |
| 117 |
| 118 | if (*got_frame) { |
| 119 | ret = write_video_frame(frame); |
| 120 | if (ret < 0) { |
| 121 | return ret; |
| 122 | } |
| 123 | } |
| 124 | } else if (pkt.stream_index == audio_stream_idx) { |
| 125 | ret = avcodec_decode_audio4(audio_dec_ctx, frame, got_frame, &pkt); |
| 126 |
| 127 | if (ret < 0) { |
| 128 | LOGD("Error decoding audio frame\n"); |
| 129 | return ret; |
| 130 | } |
| 131 | decoded = FFMIN(ret, pkt.size); |
| 132 |
| 133 | if (*got_frame) { |
| 134 | ret = write_audio_frame(frame, audio_dec_ctx); |
| 135 | if (ret < 0) { |
| 136 | return -1; |
| 137 | } |
| 138 | } |
| 139 | frame->pts = AV_NOPTS_VALUE; |
| 140 | } |
| 141 |
| 142 | if (*got_frame) { |
| 143 | av_frame_unref(frame); |
| 144 | } |
| 145 |
| 146 | return decoded; |
| 147 | } |
| 148 |
| 149 | void convertFile(const char *src_filename, const char *dst_filename, int bitr) { |
| 150 | int ret = 0; |
| 151 | int got_frame; |
| 152 |
| 153 | src = malloc(strlen(src_filename) + 1); |
| 154 | memcpy(src, src_filename, strlen(src_filename)); |
| 155 | src[strlen(src_filename)] = '\0'; |
| 156 | dest = malloc(strlen(dst_filename) + 1); |
| 157 | memcpy(dest, dst_filename, strlen(dst_filename)); |
| 158 | dest[strlen(dst_filename)] = '\0'; |
| 159 |
| 160 | if ((ret = avformat_open_input(&fmt_ctx, src, NULL, NULL)) < 0) { |
| 161 | LOGD("Could not open source file %s, %s\n", src, av_err2str(ret)); |
| 162 | onError(); |
| 163 | return; |
| 164 | } |
| 165 |
| 166 | if (avformat_find_stream_info(fmt_ctx, NULL) < 0) { |
| 167 | LOGD("Could not find stream information\n"); |
| 168 | onError(); |
| 169 | return; |
| 170 | } |
| 171 |
| 172 | if (open_codec_context(&video_stream_idx, fmt_ctx, AVMEDIA_TYPE_VIDEO) >= 0) { |
| 173 | video_stream = fmt_ctx->streams[video_stream_idx]; |
| 174 | video_dec_ctx = video_stream->codec; |
| 175 | } |
| 176 |
| 177 | if (open_codec_context(&audio_stream_idx, fmt_ctx, AVMEDIA_TYPE_AUDIO) >= 0) { |
| 178 | audio_stream = fmt_ctx->streams[audio_stream_idx]; |
| 179 | audio_dec_ctx = audio_stream->codec; |
| 180 | } |
| 181 |
| 182 | av_dump_format(fmt_ctx, 0, src, 0); |
| 183 |
| 184 | if (!audio_stream && !video_stream) { |
| 185 | LOGD("Could not find audio or video stream in the input, aborting\n"); |
| 186 | onError(); |
| 187 | return; |
| 188 | } |
| 189 |
| 190 | frame = av_frame_alloc(); |
| 191 | if (!frame) { |
| 192 | LOGD("Could not allocate frame\n"); |
| 193 | onError(); |
| 194 | return; |
| 195 | } |
| 196 |
| 197 | av_init_packet(&pkt); |
| 198 | pkt.data = NULL; |
| 199 | pkt.size = 0; |
| 200 |
| 201 | if (video_stream) { |
| 202 | LOGD("Demuxing video from file '%s'\n", src); |
| 203 | } |
| 204 | if (audio_stream) { |
| 205 | LOGD("Demuxing audio from file '%s'\n", src); |
| 206 | } |
| 207 |
| 208 | ret = prepare_for_video_conversion(dest, video_dec_ctx, audio_dec_ctx, fmt_ctx, video_stream, audio_stream, bitr); |
| 209 | if (ret < 0) { |
| 210 | return; |
| 211 | } |
| 212 | if (video_stream) { |
| 213 | total_duration = video_stream->duration; |
| 214 | } |
| 215 | if (audio_stream) { |
| 216 | total_duration += audio_stream->duration; |
| 217 | } |
| 218 |
| 219 | while (av_read_frame(fmt_ctx, &pkt) >= 0) { |
| 220 | AVPacket orig_pkt = pkt; |
| 221 | do { |
| 222 | ret = decode_packet(&got_frame, 0); |
| 223 | if (ret < 0) { |
| 224 | onError(); |
| 225 | return; |
| 226 | } |
| 227 | pkt.data += ret; |
| 228 | pkt.size -= ret; |
| 229 | current_duration += pkt.duration; |
| 230 | onProgress(); |
| 231 | } while (pkt.size > 0); |
| 232 | av_free_packet(&orig_pkt); |
| 233 | } |
| 234 |
| 235 | pkt.data = NULL; |
| 236 | pkt.size = 0; |
| 237 | do { |
| 238 | decode_packet(&got_frame, 1); |
| 239 | } while (got_frame); |
| 240 |
| 241 | post_video_conversion(); |
| 242 | onDone(); |
| 243 | } |
| 244 |