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