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