| 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->gop_size = 10; |
| 74 | ctx->codec_ctx->max_b_frames = 1; |
| 75 |
| 76 | if (ctx->fmt_ctx->oformat->flags & AVFMT_GLOBALHEADER) { |
| 77 | ctx->codec_ctx->flags |= AV_CODEC_FLAG_GLOBAL_HEADER; |
| 78 | } |
| 79 |
| 80 | ctx->video_stream = avformat_new_stream(ctx->fmt_ctx, codec); |
| 81 | if (!ctx->video_stream) { |
| 82 | LOGE("vp9: failed to create stream"); |
| 83 | return (jlong) 0; |
| 84 | } |
| 85 |
| 86 | ctx->video_stream->codecpar->codec_id = ctx->codec_ctx->codec_id; |
| 87 | ctx->video_stream->codecpar->codec_type = ctx->codec_ctx->codec_type; |
| 88 | ctx->video_stream->codecpar->width = ctx->codec_ctx->width; |
| 89 | ctx->video_stream->codecpar->height = ctx->codec_ctx->height; |
| 90 | ctx->video_stream->codecpar->format = ctx->codec_ctx->pix_fmt; |
| 91 | ctx->video_stream->time_base = ctx->codec_ctx->time_base; |
| 92 |
| 93 | ret = avcodec_open2(ctx->codec_ctx, codec, NULL); |
| 94 | if (ret < 0) { |
| 95 | LOGE("vp9: failed to open codec %s", av_err2str(ret)); |
| 96 | return (jlong) 0; |
| 97 | } |
| 98 |
| 99 | ctx->sws_ctx = sws_getContext(width, height, AV_PIX_FMT_RGBA, width, height, AV_PIX_FMT_YUVA420P, 0, 0, 0, 0); |
| 100 | if (!ctx->sws_ctx) { |
| 101 | LOGE("vp9: failed to sws_ctx"); |
| 102 | return (jlong) 0; |
| 103 | } |
| 104 |
| 105 | ctx->frame = av_frame_alloc(); |
| 106 | if (!ctx->frame) { |
| 107 | LOGE("vp9: failed to alloc frame"); |
| 108 | return (jlong)0; |
| 109 | } |
| 110 |
| 111 | ctx->frame->format = ctx->codec_ctx->pix_fmt; |
| 112 | ctx->frame->width = ctx->codec_ctx->width; |
| 113 | ctx->frame->height = ctx->codec_ctx->height; |
| 114 | ret = av_frame_get_buffer(ctx->frame, 0); |
| 115 | if (ret < 0) { |
| 116 | LOGE("vp9: failed to get frame buffer %d", ret); |
| 117 | return (jlong)0; |
| 118 | } |
| 119 |
| 120 | if (avcodec_parameters_from_context(ctx->video_stream->codecpar, ctx->codec_ctx) < 0) { |
| 121 | LOGE("vp9: failed to copy codec parameters to stream"); |
| 122 | return (jlong) 0; |
| 123 | } |
| 124 |
| 125 | ret = avformat_write_header(ctx->fmt_ctx, NULL); |
| 126 | if (ret < 0) { |
| 127 | LOGE("vp9: failed to write header %d", ret); |
| 128 | return (jlong) 0; |
| 129 | } |
| 130 |
| 131 | (*env)->ReleaseStringUTFChars(env, outputPath_, outputPath); |
| 132 |
| 133 | return (jlong)ctx; |
| 134 | } |
| 135 |
| 136 | JNIEXPORT jboolean JNICALL Java_org_telegram_messenger_video_WebmEncoder_writeFrame( |
| 137 | JNIEnv *env, jobject obj, |
| 138 | jlong ptr, |
| 139 | jobject argbPixels, |
| 140 | jint width, jint height |
| 141 | ) { |
| 142 | EncoderContext *ctx = (EncoderContext *) ptr; |
| 143 |
| 144 | uint8_t *pixels = (*env)->GetDirectBufferAddress(env, argbPixels); |
| 145 |
| 146 | if (!ctx || !pixels) { |
| 147 | LOGE("vp9: no ctx or no pixels"); |
| 148 | return JNI_FALSE; |
| 149 | } |
| 150 |
| 151 | int ret; |
| 152 | AVPacket pkt; |
| 153 | av_init_packet(&pkt); |
| 154 | pkt.data = NULL; |
| 155 | pkt.size = 0; |
| 156 |
| 157 | ret = av_frame_make_writable(ctx->frame); |
| 158 | if (ret < 0) { |
| 159 | LOGE("vp9: failed to make writable %d", ret); |
| 160 | return JNI_FALSE; |
| 161 | } |
| 162 |
| 163 | const uint8_t* srcSlice[1] = { pixels }; |
| 164 | int srcStride[1] = { 4 * width }; |
| 165 | sws_scale(ctx->sws_ctx, srcSlice, srcStride, 0, ctx->codec_ctx->height, ctx->frame->data, ctx->frame->linesize); |
| 166 |
| 167 | ctx->frame->pts = ctx->frame_count++; |
| 168 |
| 169 | ret = avcodec_send_frame(ctx->codec_ctx, ctx->frame); |
| 170 | if (ret < 0) { |
| 171 | LOGE("vp9: failed to send packet %d", ret); |
| 172 | return JNI_FALSE; |
| 173 | } |
| 174 |
| 175 | while (ret >= 0) { |
| 176 | ret = avcodec_receive_packet(ctx->codec_ctx, &pkt); |
| 177 | if (ret == AVERROR(EAGAIN) || ret == AVERROR_EOF) { |
| 178 | break; |
| 179 | } else if (ret < 0) { |
| 180 | LOGE("vp9: failed to receive packet %d", ret); |
| 181 | return JNI_FALSE; |
| 182 | } |
| 183 |
| 184 | av_packet_rescale_ts(&pkt, ctx->codec_ctx->time_base, ctx->video_stream->time_base); |
| 185 | pkt.stream_index = ctx->video_stream->index; |
| 186 |
| 187 | ret = av_interleaved_write_frame(ctx->fmt_ctx, &pkt); |
| 188 | av_packet_unref(&pkt); |
| 189 | } |
| 190 |
| 191 | return JNI_TRUE; |
| 192 | } |
| 193 |
| 194 | JNIEXPORT void JNICALL Java_org_telegram_messenger_video_WebmEncoder_stop( |
| 195 | JNIEnv *env, jobject obj, |
| 196 | jlong ptr |
| 197 | ) { |
| 198 | EncoderContext *ctx = (EncoderContext *) ptr; |
| 199 | if (!ctx || !ctx->fmt_ctx) { |
| 200 | return; |
| 201 | } |
| 202 |
| 203 | avcodec_send_frame(ctx->codec_ctx, NULL); |
| 204 | AVPacket pkt; |
| 205 | av_init_packet(&pkt); |
| 206 | pkt.data = NULL; |
| 207 | pkt.size = 0; |
| 208 |
| 209 | int ret; |
| 210 | while (1) { |
| 211 | ret = avcodec_receive_packet(ctx->codec_ctx, &pkt); |
| 212 | if (ret == AVERROR(EAGAIN) || ret == AVERROR_EOF) { |
| 213 | break; |
| 214 | } else if (ret < 0) { |
| 215 | LOGE("vp9: failed to receive packet %d", ret); |
| 216 | return; |
| 217 | } |
| 218 |
| 219 | av_packet_rescale_ts(&pkt, ctx->codec_ctx->time_base, ctx->video_stream->time_base); |
| 220 | pkt.stream_index = ctx->video_stream->index; |
| 221 |
| 222 | ret = av_interleaved_write_frame(ctx->fmt_ctx, &pkt); |
| 223 | av_packet_unref(&pkt); |
| 224 | } |
| 225 |
| 226 | av_write_trailer(ctx->fmt_ctx); |
| 227 |
| 228 | if (ctx->frame) { |
| 229 | av_frame_free(&ctx->frame); |
| 230 | } |
| 231 | if (ctx->codec_ctx) { |
| 232 | avcodec_free_context(&ctx->codec_ctx); |
| 233 | } |
| 234 | if (ctx->sws_ctx) { |
| 235 | sws_freeContext(ctx->sws_ctx); |
| 236 | } |
| 237 | if (ctx->fmt_ctx) { |
| 238 | if (!(ctx->fmt_ctx->oformat->flags & AVFMT_NOFILE)) { |
| 239 | avio_closep(&ctx->fmt_ctx->pb); |
| 240 | } |
| 241 | avformat_free_context(ctx->fmt_ctx); |
| 242 | } |
| 243 |
| 244 | free(ctx); |
| 245 | } |