| 5 | #include <libavutil/mathematics.h> |
| 6 | #include "log.h" |
| 7 |
| 8 | AVFrame *out_frame = NULL; |
| 9 | struct SwsContext *sws_ctx = NULL; |
| 10 | AVStream *video_st = NULL, *audio_st = NULL; |
| 11 | AVFormatContext *oc = NULL; |
| 12 | AVOutputFormat *fmt = NULL; |
| 13 | AVPicture dst_picture; |
| 14 |
| 15 | uint8_t **dst_samples_data = NULL; |
| 16 | SwrContext *swr_ctx = NULL; |
| 17 |
| 18 | int current_n_out = 0; |
| 19 | int current_in_buff = 0; |
| 20 | uint8_t buff[4096 * 2]; |
| 21 |
| 22 | int min(int val1, int val2) { |
| 23 | return val1 < val2 ? val1 : val2; |
| 24 | } |
| 25 |
| 26 | int prepare_for_video_conversion(const char *dst_filename, AVCodecContext *video_dec_ctx, AVCodecContext *audio_dec_ctx, AVFormatContext *fmt_ctx, AVStream *src_video_stream, AVStream *src_audio_stream, int bitr) { |
| 27 |
| 28 | if (!video_dec_ctx && !audio_dec_ctx) { |
| 29 | onError(); |
| 30 | return -1; |
| 31 | } |
| 32 |
| 33 | avformat_alloc_output_context2(&oc, NULL, "mp4", dst_filename); |
| 34 | if (!oc) { |
| 35 | onError(); |
| 36 | return -1; |
| 37 | } |
| 38 | fmt = oc->oformat; |
| 39 | av_dict_copy(&oc->metadata, fmt_ctx->metadata, 0); |
| 40 |
| 41 | int ret = 0; |
| 42 | if (!(fmt->flags & AVFMT_NOFILE)) { |
| 43 | ret = avio_open(&oc->pb, dst_filename, AVIO_FLAG_WRITE); |
| 44 | if (ret < 0) { |
| 45 | LOGD("Could not open '%s': %s\n", dst_filename, av_err2str(ret)); |
| 46 | onError(); |
| 47 | return -1; |
| 48 | } |
| 49 | } |
| 50 |
| 51 | AVCodecContext *c; |
| 52 |
| 53 | if (video_dec_ctx && src_video_stream && fmt_ctx) { |
| 54 | //calculate video resolution |
| 55 | int dst_width = video_dec_ctx->width, dst_height = video_dec_ctx->height; |
| 56 | if (video_dec_ctx->width > video_dec_ctx->height) { |
| 57 | if (video_dec_ctx->width > 480) { |
| 58 | float scale = video_dec_ctx->width / 480.0f; |
| 59 | dst_width = 480; |
| 60 | dst_height = ceilf(video_dec_ctx->height / scale); |
| 61 | } |
| 62 | } else { |
| 63 | if (video_dec_ctx->width > 480) { |
| 64 | float scale = video_dec_ctx->height / 480.0f; |
| 65 | dst_height = 480; |
| 66 | dst_width = ceilf(video_dec_ctx->width / scale); |
| 67 | } |
| 68 | } |
| 69 | if (video_dec_ctx->height != dst_height || video_dec_ctx->width != dst_width || video_dec_ctx->pix_fmt != AV_PIX_FMT_YUV420P) { |
| 70 | sws_ctx = sws_getContext(video_dec_ctx->width, video_dec_ctx->height, video_dec_ctx->pix_fmt, dst_width, dst_height, AV_PIX_FMT_YUV420P, SWS_BILINEAR, NULL, NULL, NULL); |
| 71 | if (!sws_ctx) { |
| 72 | LOGD("Could not initialize the conversion context\n"); |
| 73 | onError(); |
| 74 | return -1; |
| 75 | } |
| 76 | } |
| 77 |
| 78 | //create video stream |
| 79 | AVCodec *codec = avcodec_find_encoder(AV_CODEC_ID_MPEG4); |
| 80 | if (!codec) { |
| 81 | LOGD("Could not find encoder for '%s'\n", avcodec_get_name(AV_CODEC_ID_MPEG4)); |
| 82 | onError(); |
| 83 | return -1; |
| 84 | } |
| 85 |
| 86 | video_st = avformat_new_stream(oc, codec); |
| 87 | if (!video_st) { |
| 88 | LOGD("Could not allocate stream\n"); |
| 89 | onError(); |
| 90 | return -1; |
| 91 | } |
| 92 | video_st->id = oc->nb_streams - 1; |
| 93 | av_dict_copy(&video_st->metadata, src_video_stream->metadata, 0); |
| 94 | c = video_st->codec; |
| 95 | c->codec_id = AV_CODEC_ID_MPEG4; |
| 96 | c->bit_rate = bitr; |
| 97 | c->width = dst_width; |
| 98 | c->height = dst_height; |
| 99 | double fps = (double)src_video_stream->avg_frame_rate.num / (double)src_video_stream->avg_frame_rate.den; |
| 100 | c->time_base.den = 65535; |
| 101 | c->time_base.num = floor(65635 / fps); |
| 102 | c->gop_size = 12; |
| 103 | c->pix_fmt = AV_PIX_FMT_YUV420P; |
| 104 |
| 105 | if (oc->oformat->flags & AVFMT_GLOBALHEADER) { |
| 106 | c->flags |= CODEC_FLAG_GLOBAL_HEADER; |
| 107 | } |
| 108 |
| 109 | ret = avcodec_open2(c, codec, NULL); |
| 110 | if (ret < 0) { |
| 111 | LOGD("Could not open video codec: %s\n", av_err2str(ret)); |
| 112 | onError(); |
| 113 | return -1; |
| 114 | } |
| 115 |
| 116 | out_frame = avcodec_alloc_frame(); |
| 117 | if (!out_frame) { |
| 118 | LOGD("Could not allocate video frame\n"); |
| 119 | onError(); |
| 120 | return -1; |
| 121 | } |
| 122 |
| 123 | ret = avpicture_alloc(&dst_picture, c->pix_fmt, c->width, c->height); |
| 124 | if (ret < 0) { |
| 125 | LOGD("Could not allocate picture: %s\n", av_err2str(ret)); |
| 126 | onError(); |
| 127 | return -1; |
| 128 | } |
| 129 |
| 130 | *((AVPicture *)out_frame) = dst_picture; |
| 131 | } |
| 132 |
| 133 | //create audio stream |
| 134 | if (audio_dec_ctx && src_audio_stream) { |
| 135 | AVCodec *codec = avcodec_find_encoder(AV_CODEC_ID_AAC); |
| 136 | if (!codec) { |
| 137 | LOGD("Could not find encoder for '%s'\n", avcodec_get_name(AV_CODEC_ID_AAC)); |
| 138 | onError(); |
| 139 | return -1; |
| 140 | } |
| 141 |
| 142 | audio_st = avformat_new_stream(oc, codec); |
| 143 | if (!audio_st) { |
| 144 | LOGD("Could not allocate stream\n"); |
| 145 | onError(); |
| 146 | return -1; |
| 147 | } |
| 148 | audio_st->id = oc->nb_streams - 1; |
| 149 | av_dict_copy(&audio_st->metadata, src_audio_stream->metadata, 0); |
| 150 | c = audio_st->codec; |
| 151 | c->sample_fmt = AV_SAMPLE_FMT_FLTP; |
| 152 | c->bit_rate = 40000; |
| 153 | c->sample_rate = min(audio_dec_ctx->sample_rate, 44100); |
| 154 | c->channels = 1; |
| 155 |
| 156 | if (oc->oformat->flags & AVFMT_GLOBALHEADER) { |
| 157 | c->flags |= CODEC_FLAG_GLOBAL_HEADER; |
| 158 | } |
| 159 |
| 160 | c = audio_st->codec; |
| 161 | c->strict_std_compliance = -2; |
| 162 |
| 163 | swr_ctx = swr_alloc_set_opts(NULL, AV_CH_LAYOUT_MONO, c->sample_fmt, c->sample_rate, audio_dec_ctx->channel_layout, audio_dec_ctx->sample_fmt, audio_dec_ctx->sample_rate, 0, NULL); |
| 164 | if (!swr_ctx) { |
| 165 | LOGD("Could not allocate resampler context\n"); |
| 166 | onError(); |
| 167 | return -1; |
| 168 | } |
| 169 |
| 170 | if ((ret = swr_init(swr_ctx)) < 0) { |
| 171 | LOGD("Failed to initialize the resampling context\n"); |
| 172 | onError(); |
| 173 | return -1; |
| 174 | } |
| 175 |
| 176 | ret = avcodec_open2(c, codec, NULL); |
| 177 | if (ret < 0) { |
| 178 | LOGD("Could not open audio codec: %s\n", av_err2str(ret)); |
| 179 | onError(); |
| 180 | return -1; |
| 181 | } |
| 182 |
| 183 | av_dump_format(oc, 0, dst_filename, 1); |
| 184 |
| 185 | ret = avformat_write_header(oc, NULL); |
| 186 | if (ret < 0) { |
| 187 | LOGD("Error occurred when opening output file: %s\n", av_err2str(ret)); |
| 188 | onError(); |
| 189 | return -1; |
| 190 | } |
| 191 |
| 192 | if (out_frame) { |
| 193 | out_frame->pts = 0; |
| 194 | } |
| 195 | } |
| 196 | return 0; |
| 197 | } |
| 198 |
| 199 | void cleanup_out() { |
| 200 | if (video_st) { |
| 201 | avcodec_close(video_st->codec); |
| 202 | if (dst_picture.data) { |
| 203 | av_free(dst_picture.data[0]); |
| 204 | } |
| 205 | if (out_frame) { |
| 206 | av_free(out_frame); |
| 207 | out_frame = NULL; |
| 208 | } |
| 209 | video_st = NULL; |
| 210 | } |
| 211 | if (audio_st) { |
| 212 | avcodec_close(audio_st->codec); |
| 213 | if (dst_samples_data) { |
| 214 | av_free(dst_samples_data[0]); |
| 215 | dst_samples_data = NULL; |
| 216 | } |
| 217 | audio_st = NULL; |
| 218 | } |
| 219 |
| 220 | if (fmt && !(fmt->flags & AVFMT_NOFILE)) { |
| 221 | avio_close(oc->pb); |
| 222 | fmt = NULL; |
| 223 | } |
| 224 |
| 225 | if (oc) { |
| 226 | avformat_free_context(oc); |
| 227 | oc = NULL; |
| 228 | } |
| 229 | if (sws_ctx) { |
| 230 | sws_freeContext(sws_ctx); |
| 231 | sws_ctx = NULL; |
| 232 | } |
| 233 | if (swr_ctx) { |
| 234 | swr_free(&swr_ctx); |
| 235 | swr_ctx = NULL; |
| 236 | } |
| 237 | current_n_out = 0; |
| 238 | current_in_buff = 0; |
| 239 | } |
| 240 |
| 241 | int write_video_frame(AVFrame *src_frame) { |
| 242 | int ret; |
| 243 |
| 244 | if (sws_ctx) { |
| 245 | ret = sws_scale(sws_ctx, (const uint8_t * const *)src_frame->data, src_frame->linesize, 0, src_frame->height, out_frame->data, out_frame->linesize); |
| 246 | if (ret < 0) { |
| 247 | LOGD("scale error: %s\n", av_err2str(ret)); |
| 248 | onError(); |
| 249 | return -1; |
| 250 | } |
| 251 | } else { |
| 252 | for (int i = 0; i < 4; i++){ |
| 253 | out_frame->data[i] = src_frame->data[i]; |
| 254 | out_frame->linesize[i] = src_frame->linesize[i]; |
| 255 | } |
| 256 | } |
| 257 |
| 258 | AVPacket pkt = { 0 }; |
| 259 | int got_packet; |
| 260 | av_init_packet(&pkt); |
| 261 |
| 262 | ret = avcodec_encode_video2(video_st->codec, &pkt, out_frame, &got_packet); |
| 263 | if (ret < 0) { |
| 264 | LOGD("Error encoding video frame: %s\n", av_err2str(ret)); |
| 265 | onError(); |
| 266 | return -1; |
| 267 | } |
| 268 |
| 269 | if (!ret && got_packet && pkt.size) { |
| 270 | pkt.stream_index = video_st->index; |
| 271 | ret = av_interleaved_write_frame(oc, &pkt); |
| 272 | } else { |
| 273 | ret = 0; |
| 274 | } |
| 275 |
| 276 | if (ret != 0) { |
| 277 | LOGD("Error while writing video frame: %s\n", av_err2str(ret)); |
| 278 | onError(); |
| 279 | return -1; |
| 280 | } |
| 281 | int64_t val = av_rescale_q(1, video_st->codec->time_base, video_st->time_base); |
| 282 | out_frame->pts += val; |
| 283 | return 0; |
| 284 | } |
| 285 |
| 286 | int check_write_packet(int flush) { |
| 287 | int got_packet, ret; |
| 288 | int writed = 0; |
| 289 | int dst_samples_size = av_samples_get_buffer_size(NULL, audio_st->codec->channels, audio_st->codec->frame_size, audio_st->codec->sample_fmt, 1); |
| 290 | while (current_n_out > audio_st->codec->frame_size || (flush && current_n_out)) { |
| 291 | AVFrame *frame = avcodec_alloc_frame(); |
| 292 | AVPacket pkt2 = { 0 }; |
| 293 | av_init_packet(&pkt2); |
| 294 |
| 295 | frame->nb_samples = min(audio_st->codec->frame_size, current_n_out); |
| 296 | int nb_samples_size = min(dst_samples_size, current_in_buff); |
| 297 | ret = avcodec_fill_audio_frame(frame, audio_st->codec->channels, audio_st->codec->sample_fmt, buff + writed, nb_samples_size, 1); |
| 298 |
| 299 | if (ret < 0) { |
| 300 | LOGD("Error fill frame: %s\n", av_err2str(ret)); |
| 301 | onError(); |
| 302 | return -1; |
| 303 | } |
| 304 |
| 305 | ret = avcodec_encode_audio2(audio_st->codec, &pkt2, frame, &got_packet); |
| 306 | if (ret < 0) { |
| 307 | LOGD("Error encoding audio frame: %s\n", av_err2str(ret)); |
| 308 | onError(); |
| 309 | return -1; |
| 310 | } |
| 311 |
| 312 | if (got_packet) { |
| 313 | pkt2.stream_index = audio_st->index; |
| 314 | ret = av_interleaved_write_frame(oc, &pkt2); |
| 315 | if (ret != 0) { |
| 316 | LOGD("Error while writing audio frame: %s\n", av_err2str(ret)); |
| 317 | onError(); |
| 318 | return -1; |
| 319 | } |
| 320 | } |
| 321 | writed += dst_samples_size; |
| 322 | current_n_out -= frame->nb_samples; |
| 323 | current_in_buff -= nb_samples_size; |
| 324 | avcodec_free_frame(&frame); |
| 325 | } |
| 326 | if (current_in_buff != 0 && writed != 0) { |
| 327 | memcpy(buff, buff + writed, current_in_buff); |
| 328 | } |
| 329 | return 0; |
| 330 | } |
| 331 |
| 332 | int write_audio_frame(AVFrame *src_frame, AVCodecContext *src_codec) { |
| 333 | const int n_in = src_frame->nb_samples; |
| 334 | double ratio = (double)audio_st->codec->sample_rate / src_frame->sample_rate; |
| 335 | int n_out = n_in * ratio + 32; |
| 336 | int64_t delay = swr_get_delay(swr_ctx, audio_st->codec->sample_rate); |
| 337 | if (delay > 0) { |
| 338 | n_out += delay; |
| 339 | } |
| 340 |
| 341 | if (!dst_samples_data) { |
| 342 | int ret = av_samples_alloc_array_and_samples(&dst_samples_data, NULL, audio_st->codec->channels, n_out, audio_st->codec->sample_fmt, 0); |
| 343 | if (ret < 0) { |
| 344 | LOGD("Could not allocate destination samples\n"); |
| 345 | onError(); |
| 346 | return -1; |
| 347 | } |
| 348 | } |
| 349 |
| 350 | n_out = swr_convert(swr_ctx, dst_samples_data, n_out, (const uint8_t **)src_frame->extended_data, src_frame->nb_samples); |
| 351 | if (n_out <= 0) { |
| 352 | LOGD("Error while converting\n"); |
| 353 | onError(); |
| 354 | return -1; |
| 355 | } |
| 356 | int total_size = av_samples_get_buffer_size(NULL, audio_st->codec->channels, n_out, audio_st->codec->sample_fmt, 1); |
| 357 | memcpy(buff + current_in_buff, dst_samples_data[0], total_size); |
| 358 | current_n_out += n_out; |
| 359 | current_in_buff += total_size; |
| 360 | return check_write_packet(0); |
| 361 | } |
| 362 |
| 363 | void post_video_conversion() { |
| 364 | check_write_packet(1); |
| 365 | av_write_trailer(oc); |
| 366 | } |
| 367 |