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