| 5 | #include "libavcodec/avcodec.h" |
| 6 | #include "c_utils.h" |
| 7 | #include "libavutil/opt.h" |
| 8 | #include "libswscale/swscale.h" |
| 9 |
| 10 | typedef struct { |
| 11 | AVCodecContext *codec_ctx; |
| 12 | AVFormatContext *fmt_ctx; |
| 13 | AVStream *video_stream; |
| 14 | AVFrame *frame; |
| 15 | int frame_count; |
| 16 | struct SwsContext *sws_ctx; |
| 17 | } EncoderContext; |
| 18 |
| 19 | static void free_encoder(EncoderContext *ctx) { |
| 20 | if (!ctx) return; |
| 21 | if (ctx->frame) av_frame_free(&ctx->frame); |
| 22 | if (ctx->codec_ctx) avcodec_free_context(&ctx->codec_ctx); |
| 23 | if (ctx->sws_ctx) sws_freeContext(ctx->sws_ctx); |
| 24 | if (ctx->fmt_ctx) { |
| 25 | if (!(ctx->fmt_ctx->oformat->flags & AVFMT_NOFILE)) { |
| 26 | avio_closep(&ctx->fmt_ctx->pb); |
| 27 | } |
| 28 | avformat_free_context(ctx->fmt_ctx); |
| 29 | } |
| 30 | free(ctx); |
| 31 | } |
| 32 |
| 33 | JNIEXPORT jlong JNICALL Java_org_telegram_messenger_video_WebmEncoder_createEncoder( |
| 34 | JNIEnv *env, jobject obj, |
| 35 | jstring outputPath_, |
| 36 | jint width, jint height, |
| 37 | jint fps, |
| 38 | jlong bitrate |
| 39 | ) { |
| 40 |
| 41 | int ret; |
| 42 | const char* outputPath = (*env)->GetStringUTFChars(env, outputPath_, 0); |
| 43 |
| 44 | EncoderContext* ctx = (EncoderContext*) malloc(sizeof(EncoderContext)); |
| 45 | if (!ctx) { |
| 46 | LOGE("vp9: failed to alloc context"); |
| 47 | goto cleanup; |
| 48 | } |
| 49 | memset(ctx, 0, sizeof(EncoderContext)); |
| 50 |
| 51 | avformat_alloc_output_context2(&ctx->fmt_ctx, NULL, "matroska", outputPath); |
| 52 | if (!ctx->fmt_ctx) { |
| 53 | LOGE("vp9: no context created!"); |
| 54 | goto cleanup; |
| 55 | } |
| 56 |
| 57 | if (!(ctx->fmt_ctx->oformat->flags & AVFMT_NOFILE)) { |
| 58 | ret = avio_open(&ctx->fmt_ctx->pb, outputPath, AVIO_FLAG_WRITE); |
| 59 | if (ret < 0) { |
| 60 | LOGE("vp9: failed to write open file %d", ret); |
| 61 | goto cleanup; |
| 62 | } |
| 63 | } |
| 64 |
| 65 | AVCodec* codec = avcodec_find_encoder(AV_CODEC_ID_VP9); |
| 66 | if (!codec) { |
| 67 | LOGE("vp9: no encoder found!"); |
| 68 | goto cleanup; |
| 69 | } |
| 70 |
| 71 | ctx->codec_ctx = avcodec_alloc_context3(codec); |
| 72 | if (!ctx->codec_ctx) { |
| 73 | LOGE("vp9: failed to create codec ctx"); |
| 74 | goto cleanup; |
| 75 | } |
| 76 |
| 77 | ctx->codec_ctx->codec_id = AV_CODEC_ID_VP9; |
| 78 | ctx->codec_ctx->codec_type = AVMEDIA_TYPE_VIDEO; |
| 79 | ctx->codec_ctx->width = width; |
| 80 | ctx->codec_ctx->height = height; |
| 81 | ctx->codec_ctx->pix_fmt = AV_PIX_FMT_YUVA420P; |
| 82 | ctx->codec_ctx->color_range = AVCOL_RANGE_MPEG; |
| 83 | ctx->codec_ctx->color_primaries = AVCOL_PRI_BT709; |
| 84 | ctx->codec_ctx->colorspace = AVCOL_SPC_BT709; |
| 85 | ctx->codec_ctx->time_base = (AVRational){ 1, fps }; |
| 86 | ctx->codec_ctx->framerate = (AVRational){ fps, 1 }; |
| 87 | ctx->codec_ctx->bit_rate = bitrate; |
| 88 | ctx->codec_ctx->rc_min_rate = bitrate / 8; |
| 89 | ctx->codec_ctx->rc_max_rate = bitrate; |
| 90 | // ctx->codec_ctx->rc_buffer_size = 2 * bitrate; |
| 91 |
| 92 | if (ctx->fmt_ctx->oformat->flags & AVFMT_GLOBALHEADER) { |
| 93 | ctx->codec_ctx->flags |= AV_CODEC_FLAG_GLOBAL_HEADER; |
| 94 | } |
| 95 |
| 96 | ctx->video_stream = avformat_new_stream(ctx->fmt_ctx, codec); |
| 97 | if (!ctx->video_stream) { |
| 98 | LOGE("vp9: failed to create stream"); |
| 99 | goto cleanup; |
| 100 | } |
| 101 |
| 102 | ctx->video_stream->codecpar->codec_id = ctx->codec_ctx->codec_id; |
| 103 | ctx->video_stream->codecpar->codec_type = ctx->codec_ctx->codec_type; |
| 104 | ctx->video_stream->codecpar->width = ctx->codec_ctx->width; |
| 105 | ctx->video_stream->codecpar->height = ctx->codec_ctx->height; |
| 106 | ctx->video_stream->codecpar->format = ctx->codec_ctx->pix_fmt; |
| 107 | ctx->video_stream->time_base = ctx->codec_ctx->time_base; |
| 108 |
| 109 | ret = avcodec_open2(ctx->codec_ctx, codec, NULL); |
| 110 | if (ret < 0) { |
| 111 | LOGE("vp9: failed to open codec %s", av_err2str(ret)); |
| 112 | goto cleanup; |
| 113 | } |
| 114 |
| 115 | ctx->sws_ctx = sws_getContext(width, height, AV_PIX_FMT_RGBA, width, height, AV_PIX_FMT_YUVA420P, 0, 0, 0, 0); |
| 116 | if (!ctx->sws_ctx) { |
| 117 | LOGE("vp9: failed to sws_ctx"); |
| 118 | goto cleanup; |
| 119 | } |
| 120 |
| 121 | ctx->frame = av_frame_alloc(); |
| 122 | if (!ctx->frame) { |
| 123 | LOGE("vp9: failed to alloc frame"); |
| 124 | goto cleanup; |
| 125 | } |
| 126 |
| 127 | ctx->frame->format = ctx->codec_ctx->pix_fmt; |
| 128 | ctx->frame->width = ctx->codec_ctx->width; |
| 129 | ctx->frame->height = ctx->codec_ctx->height; |
| 130 | ret = av_frame_get_buffer(ctx->frame, 0); |
| 131 | if (ret < 0) { |
| 132 | LOGE("vp9: failed to get frame buffer %d", ret); |
| 133 | goto cleanup; |
| 134 | } |
| 135 |
| 136 | if (avcodec_parameters_from_context(ctx->video_stream->codecpar, ctx->codec_ctx) < 0) { |
| 137 | LOGE("vp9: failed to copy codec parameters to stream"); |
| 138 | goto cleanup; |
| 139 | } |
| 140 |
| 141 | ret = avformat_write_header(ctx->fmt_ctx, NULL); |
| 142 | if (ret < 0) { |
| 143 | LOGE("vp9: failed to write header %d", ret); |
| 144 | goto cleanup; |
| 145 | } |
| 146 |
| 147 | (*env)->ReleaseStringUTFChars(env, outputPath_, outputPath); |
| 148 |
| 149 | return (jlong)ctx; |
| 150 |
| 151 | cleanup: |
| 152 | (*env)->ReleaseStringUTFChars(env, outputPath_, outputPath); |
| 153 | free_encoder(ctx); |
| 154 | return (jlong) 0; |
| 155 | } |
| 156 |
| 157 | JNIEXPORT jboolean JNICALL Java_org_telegram_messenger_video_WebmEncoder_writeFrame( |
| 158 | JNIEnv *env, jobject obj, |
| 159 | jlong ptr, |
| 160 | jobject argbPixels, |
| 161 | jint width, jint height |
| 162 | ) { |
| 163 | EncoderContext *ctx = (EncoderContext *) ptr; |
| 164 |
| 165 | uint8_t *pixels = (*env)->GetDirectBufferAddress(env, argbPixels); |
| 166 |
| 167 | if (!ctx || !pixels) { |
| 168 | LOGE("vp9: no ctx or no pixels"); |
| 169 | return JNI_FALSE; |
| 170 | } |
| 171 |
| 172 | int ret; |
| 173 | AVPacket pkt; |
| 174 | av_init_packet(&pkt); |
| 175 | pkt.data = NULL; |
| 176 | pkt.size = 0; |
| 177 |
| 178 | ret = av_frame_make_writable(ctx->frame); |
| 179 | if (ret < 0) { |
| 180 | LOGE("vp9: failed to make writable %d", ret); |
| 181 | return JNI_FALSE; |
| 182 | } |
| 183 |
| 184 | const uint8_t* srcSlice[1] = { pixels }; |
| 185 | int srcStride[1] = { 4 * width }; |
| 186 | sws_scale(ctx->sws_ctx, srcSlice, srcStride, 0, ctx->codec_ctx->height, ctx->frame->data, ctx->frame->linesize); |
| 187 |
| 188 | ctx->frame->pts = ctx->frame_count++; |
| 189 |
| 190 | ret = avcodec_send_frame(ctx->codec_ctx, ctx->frame); |
| 191 | if (ret < 0) { |
| 192 | LOGE("vp9: failed to send packet %d", ret); |
| 193 | return JNI_FALSE; |
| 194 | } |
| 195 |
| 196 | while (ret >= 0) { |
| 197 | ret = avcodec_receive_packet(ctx->codec_ctx, &pkt); |
| 198 | if (ret == AVERROR(EAGAIN) || ret == AVERROR_EOF) { |
| 199 | break; |
| 200 | } else if (ret < 0) { |
| 201 | LOGE("vp9: failed to receive packet %d", ret); |
| 202 | return JNI_FALSE; |
| 203 | } |
| 204 |
| 205 | av_packet_rescale_ts(&pkt, ctx->codec_ctx->time_base, ctx->video_stream->time_base); |
| 206 | pkt.stream_index = ctx->video_stream->index; |
| 207 |
| 208 | ret = av_interleaved_write_frame(ctx->fmt_ctx, &pkt); |
| 209 | if (ret < 0) { |
| 210 | LOGE("vp9: failed to av_interleaved_write_frame %d", ret); |
| 211 | } |
| 212 | av_packet_unref(&pkt); |
| 213 | } |
| 214 |
| 215 | return JNI_TRUE; |
| 216 | } |
| 217 |
| 218 | JNIEXPORT void JNICALL Java_org_telegram_messenger_video_WebmEncoder_stop( |
| 219 | JNIEnv *env, jobject obj, |
| 220 | jlong ptr |
| 221 | ) { |
| 222 | EncoderContext *ctx = (EncoderContext *) ptr; |
| 223 | if (!ctx || !ctx->fmt_ctx || !ctx->codec_ctx) { |
| 224 | free_encoder(ctx); |
| 225 | return; |
| 226 | } |
| 227 |
| 228 | int ret; |
| 229 | ret = avcodec_send_frame(ctx->codec_ctx, NULL); |
| 230 | if (ret < 0) { |
| 231 | LOGE("vp9: failed to avcodec_send_frame %d", ret); |
| 232 | } |
| 233 | AVPacket pkt; |
| 234 | av_init_packet(&pkt); |
| 235 | pkt.data = NULL; |
| 236 | pkt.size = 0; |
| 237 |
| 238 | while (1) { |
| 239 | ret = avcodec_receive_packet(ctx->codec_ctx, &pkt); |
| 240 | if (ret == AVERROR(EAGAIN) || ret == AVERROR_EOF) { |
| 241 | break; |
| 242 | } else if (ret < 0) { |
| 243 | LOGE("vp9: failed to receive packet %d", ret); |
| 244 | return; |
| 245 | } |
| 246 |
| 247 | av_packet_rescale_ts(&pkt, ctx->codec_ctx->time_base, ctx->video_stream->time_base); |
| 248 | pkt.stream_index = ctx->video_stream->index; |
| 249 |
| 250 | ret = av_interleaved_write_frame(ctx->fmt_ctx, &pkt); |
| 251 | if (ret < 0) { |
| 252 | LOGE("vp9: failed to av_interleaved_write_frame %d", ret); |
| 253 | } |
| 254 | av_packet_unref(&pkt); |
| 255 | } |
| 256 |
| 257 | ret = av_write_trailer(ctx->fmt_ctx); |
| 258 | if (ret < 0) { |
| 259 | LOGE("vp9: failed to av_write_trailer %d", ret); |
| 260 | } |
| 261 |
| 262 | free_encoder(ctx); |
| 263 | } |
| 264 |