int main() { char buf[1024] ={ "我住长江头,君住长江尾。\n" "日日思君不见君,共饮长江水。\n" "此水几时休?此恨何时已?\n" "只愿君心似我心,定不负相思意。\n" }; int ret; FILE * fp = fopen("卜算子.log", "w+"); if(fp == NULL ) { perror("fopen"); return 0; } fwrite(buf, 1, strlen(buf), fp); flose(fp); return 0; }
/** 解码指定文件到目标路径 */ JNIEXPORT jint JNICALL Java_com_linecloud_composition_jni_LYCompositionKit_decodeForYUV (JNIEnv *env, jclass clazz, jstring input_jstr, jstring output_jstr) { AVFormatContext *pFormatCtx; int i; int videoindex; AVCodecContext *pCodecCtx; AVCodec *pCodec; AVFrame *pFrame; AVFrame *pFrameYUV; uint8_t *out_buffer; AVPacket *packet; int y_size; int ret; int got_picture; struct SwsContext *img_convert_ctx; FILE *fp_yuv; int frame_cnt; clock_t time_start; clock_t time_finish; double time_duration = 0.0; char input_str[500] = {0}; char output_str[500] = {0}; char info[1000] = {0}; sprintf(input_str, "%s", (*env)->GetStringUTFChars(env, input_jstr, NULL)); sprintf(output_str, "%s", (*env)->GetStringUTFChars(env, output_jstr, NULL)); //FFmpeg av_log() callback av_log_set_callback(custom_log); // log av_register_all(); avformat_network_init(); pFormatCtx = avformat_alloc_context(); if(avformat_open_input(&pFormatCtx, input_str, NULL, NULL) != 0) { LOGE("Couldn't open input stream.\n"); return -1; } if(avformat_find_stream_info(pFormatCtx, NULL) < 0) { LOGE("Couldn't find stream information.\n"); return -1; } videoindex = -1; for(i = 0; i < pFormatCtx->nb_streams; i++) { if(pFormatCtx->streams[i]->codec->codec_type == AVMEDIA_TYPE_VIDEO) { videoindex = i; break; } } if(videoindex == -1) { LOGE("Couldn't find a video stream.\n"); return -1; } pCodecCtx = pFormatCtx->streams[videoindex]->codec; pCodec = avcodec_find_decoder(pCodecCtx->codec_id); if(pCodec == NULL) { LOGE("Couldn't open codec.\n"); return -1; } if(avcodec_open2(pCodecCtx, pCodec, NULL) < 0) { LOGE("Couldn't open codec.\n"); return -1; } pFrame = av_frame_alloc(); pFrameYUV = av_frame_alloc(); out_buffer = (unsigned char *)av_malloc(av_image_get_buffer_size(AV_PIX_FMT_YUV420P, pCodecCtx->width, pCodecCtx->height, 1)); av_image_fill_arrays(pFrameYUV->data, pFrameYUV->linesize, out_buffer, AV_PIX_FMT_YUV420P, pCodecCtx->width, pCodecCtx->height, 1); packet = (AVPacket *) av_malloc(sizeof(AVPacket)); img_convert_ctx = sws_getContext(pCodecCtx->width, pCodecCtx->height, pCodecCtx->pix_fmt, pCodecCtx->width, pCodecCtx->height, AV_PIX_FMT_YUV420P, SWS_BICUBIC, NULL, NULL, NULL); sprintf(info, "[Input ]%s\n", input_str); sprintf(info, "%s[Output ]%s\n", info, output_str); sprintf(info, "%s[Format ]%s\n", info, pFormatCtx->iformat->name); sprintf(info, "%s[Codec ]%s\n", info, pCodecCtx->codec->name); sprintf(info, "%s[Resolution]%dx%d\n", info, pCodecCtx->width, pCodecCtx->height); fp_yuv = fopen(output_str, "wb+"); if (fp_yuv == NULL) { LOGE("Couldn't open output file.\n"); return -1; } frame_cnt = 0; time_start = clock(); while(av_read_frame(pFormatCtx, packet) >= 0) { if(packet->stream_index == videoindex) { ret = avcodec_decode_video2(pCodecCtx, pFrame, &got_picture, packet); if(ret < 0) { LOGE("Decode error.\n"); return -1; } if(got_picture) { // 缩放图片处理 sws_scale(img_convert_ctx, (const uint8_t* const*)pFrame->data, pFrame->linesize, 0, pCodecCtx->height, pFrameYUV->data, pFrame->linesize); y_size = pCodecCtx->width * pCodecCtx->height; fwrite(pFrameYUV->data[0], 1, y_size, fp_yuv);// Y fwrite(pFrameYUV->data[1], 1, y_size, fp_yuv);// U fwrite(pFrameYUV->data[2], 1, y_size, fp_yuv);// V // output info char picture_str[10] = {0}; switch(pFrame->pict_type) { case AV_PICTURE_TYPE_I: sprintf(picture_str, "I"); break; case AV_PICTURE_TYPE_B: sprintf(picture_str, "B"); break; default: sprintf(picture_str, "Other"); break; } LOGI("Frame Index: %5d. Type:%s", frame_cnt, picture_str); frame_cnt++; } } av_free_packet(packet); } // flush decoder // FIX: flush frames remained in decodec while(1) { ret = avcodec_decode_video2(pCodecCtx, pFrame, &got_picture, packet); if(ret < 0) { break; } if(!got_picture) { break; } sws_scale(img_convert_ctx, (const uint8_t* const*)pFrame->data, pFrame->linesize, 0, pCodecCtx->height, pFrameYUV->data, pFrameYUV->linesize); int y_size = pCodecCtx->width * pCodecCtx->height; fwrite(pFrameYUV->data[0], 1, y_size, fp_yuv);// Y fwrite(pFrameYUV->data[1], 1, y_size, fp_yuv);// U fwrite(pFrameYUV->data[2], 1, y_size, fp_yuv);// V // output info char picture_str[10] = {0}; switch(pFrame->pict_type) { case AV_PICTURE_TYPE_I: sprintf(picture_str, "I"); break; case AV_PICTURE_TYPE_P: sprintf(picture_str, "P"); break; case AV_PICTURE_TYPE_B: sprintf(picture_str, "B"); break; default: sprintf(picture_str, "Other"); break; } LOGI("Frame Index: %5d. Type:%s", frame_cnt, picture_str); frame_cnt++; } time_finish = clock(); time_duration = (double)(time_finish - time_start); sprintf(info, "%s[Time ]%f ms\n", info, time_duration); sprintf(info, "%s[Count ]%d\n", info, frame_cnt); sws_freeContext(img_convert_ctx); flose(fp_yuv); av_frame_free(&pFrameYUV); av_frame_free(&pFrame); avcodec_close(pCodecCtx); avfromat_close_input(&pFormatCtx); return 0; }