├── .gitignore ├── .idea ├── .name ├── misc.xml ├── modules.xml ├── opencvideo-master.iml ├── vcs.xml └── workspace.xml ├── 264rawFrame.tar.gz ├── CMakeLists.txt ├── H264Decoder.cpp ├── H264Decoder.h ├── H264Decoder.java ├── H264Decoder_jni.cpp ├── readme.md └── sample.cpp /.gitignore: -------------------------------------------------------------------------------- 1 | # Prerequisites 2 | *.d 3 | 4 | # Compiled Object files 5 | *.slo 6 | *.lo 7 | *.o 8 | *.obj 9 | 10 | # Precompiled Headers 11 | *.gch 12 | *.pch 13 | 14 | # Compiled Dynamic libraries 15 | *.so 16 | *.dylib 17 | *.dll 18 | 19 | # Fortran module files 20 | *.mod 21 | *.smod 22 | 23 | # Compiled Static libraries 24 | *.lai 25 | *.la 26 | *.a 27 | *.lib 28 | 29 | # Executables 30 | *.exe 31 | *.out 32 | *.app 33 | 34 | *~ 35 | *264 36 | /cmake-build-debug 37 | /CMakeFiles 38 | -------------------------------------------------------------------------------- /.idea/.name: -------------------------------------------------------------------------------- 1 | H264Decoder -------------------------------------------------------------------------------- /.idea/misc.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | -------------------------------------------------------------------------------- /.idea/modules.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | -------------------------------------------------------------------------------- /.idea/opencvideo-master.iml: -------------------------------------------------------------------------------- 1 | 2 | -------------------------------------------------------------------------------- /.idea/vcs.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | -------------------------------------------------------------------------------- /.idea/workspace.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 29 | 30 | 31 | 32 | 33 | 34 | 35 | 36 | 37 | 38 | 39 | 40 | 41 | 42 | 43 | 44 | 45 | 46 | 47 | 48 | 49 | 50 | 51 | 52 | 53 | 54 | 55 | 56 | 57 | 58 | 59 | 60 | 61 | 62 | 63 | 64 | 65 | 66 | 67 | 68 | 69 | 70 | 71 | 72 | 73 | 74 | 75 | 76 | 77 | 78 | 79 | 80 | 81 | 82 | 83 | 84 | 85 | 86 | 87 | 88 | 89 | 90 | 91 | 92 | 93 | 94 | 95 | 96 | 97 | 98 | 99 | 100 | cascade 101 | pCvMat 102 | out_buffer 103 | pFrameBGR 104 | gray 105 | avcodec_alloc_ 106 | pAvFrame 107 | AVCodecContext 108 | frame_size 109 | frame 110 | INBUF_SIZE 111 | AVPacket 112 | Picture 113 | avcodec_decode_video 114 | size 115 | packet 116 | pCodecCtx 117 | rame_c 118 | pFormatCtx 119 | frame_count 120 | 121 | 122 | 123 | 125 | 126 | 141 | 142 | 143 | 144 | 145 | true 146 | DEFINITION_ORDER 147 | 148 | 149 | 150 | 155 | 156 | 157 | 158 | 159 | 160 | 161 | 162 | 163 | 164 | 165 | 166 | 167 | 168 | 169 | 170 | 171 | 172 | 173 | 174 | 175 | 178 | 179 | 182 | 183 | 184 | 185 | 186 | 187 | 188 | 189 | 190 | 191 | 192 | 193 | 194 | 195 | 196 | 197 | 207 | 208 | 209 | 210 | 211 | 212 | 213 | 214 | 215 | 216 | 217 | 218 | 219 | 220 | 221 | 222 | 223 | 224 | 225 | 226 | 227 | 228 | 229 | 230 | 231 | 232 | 233 | 234 | 235 | 236 | 237 | 238 | 239 | 240 | 241 | 242 | 243 | 244 | 245 | 246 | 247 | 248 | 249 | 250 | 251 | 252 | 253 | 270 | 271 | 272 | 273 | 274 | 275 | 276 | 277 | 288 | 289 | 304 | 305 | 306 | 307 | 308 | 309 | 310 | project 311 | 312 | 313 | 314 | 315 | 316 | 317 | 318 | 319 | 320 | 321 | 322 | 323 | 324 | 325 | 326 | 344 | 345 | 360 | 361 | 375 | 376 | 391 | 392 | 393 | 394 | 395 | 396 | 397 | 398 | 399 | 400 | 401 | 402 | 403 | 404 | 405 | 406 | 407 | 408 | 409 | 410 | 412 | 413 | 414 | 415 | 1498113401750 416 | 430 | 431 | 432 | 433 | 435 | 436 | 437 | 438 | 439 | 440 | 441 | 442 | 443 | 444 | 445 | 446 | 447 | 448 | 449 | 450 | 451 | 452 | 453 | 454 | 455 | 456 | 457 | 458 | 459 | 460 | 461 | 463 | 464 | 466 | 467 | 468 | 469 | 470 | file://$PROJECT_DIR$/getYUVFrame.cpp 471 | 196 472 | 474 | 475 | file://$PROJECT_DIR$/getYUVFrame.cpp 476 | 212 477 | 479 | 480 | file://$PROJECT_DIR$/getYUVFrame.cpp 481 | 254 482 | 484 | 485 | file://$PROJECT_DIR$/openheader.cpp 486 | 159 487 | 489 | 490 | 492 | 493 | 494 | 495 | 496 | 497 | 498 | 499 | 500 | 501 | 502 | 503 | 504 | 505 | 506 | 507 | 508 | 509 | 510 | 511 | 512 | 513 | 514 | 515 | 516 | 517 | 518 | 519 | 520 | 521 | 522 | 523 | 524 | 525 | 526 | 527 | 528 | 529 | 530 | 531 | 532 | 533 | 534 | 535 | 536 | 537 | 538 | 539 | 540 | 541 | 542 | 543 | 544 | 545 | 546 | 547 | 548 | 549 | 550 | 551 | 552 | 553 | 554 | 555 | 556 | 557 | 558 | 559 | 560 | 561 | 562 | 563 | 564 | 565 | 566 | 567 | 568 | 569 | 570 | 571 | 572 | 573 | 574 | 575 | 576 | 577 | 578 | 579 | 580 | 581 | 582 | 583 | 584 | 585 | 586 | 587 | 588 | 589 | 590 | 591 | 592 | 593 | 594 | 595 | 596 | 597 | 598 | 599 | 600 | 601 | 602 | 603 | 604 | 605 | 606 | 607 | 608 | 609 | 610 | 611 | 612 | 613 | 614 | 615 | 616 | 617 | 618 | 619 | 620 | 621 | 622 | 623 | 624 | 625 | 626 | 627 | 628 | 629 | 630 | 631 | 632 | 633 | 634 | 635 | 636 | 637 | 638 | 639 | 640 | 641 | 642 | 643 | 644 | 645 | 646 | 647 | 648 | 649 | 650 | 651 | 652 | 653 | 654 | 655 | 656 | 657 | 658 | 659 | 660 | 661 | 662 | 663 | 664 | 665 | 666 | 667 | 668 | 669 | 670 | 671 | 672 | 673 | 674 | 675 | 676 | 677 | 678 | 679 | 680 | 681 | 682 | 683 | 684 | 685 | 686 | 687 | 688 | 689 | 690 | 691 | 692 | 693 | 694 | 695 | 696 | 697 | 698 | 699 | 700 | 701 | 702 | 703 | 704 | 705 | 706 | 707 | -------------------------------------------------------------------------------- /264rawFrame.tar.gz: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/hirorogithub/ffmpeg_sample-H264_to_cv-Mat/0f46a26ebf0254846c8b88d07e27e9cc0a1ef846/264rawFrame.tar.gz -------------------------------------------------------------------------------- /CMakeLists.txt: -------------------------------------------------------------------------------- 1 | cmake_minimum_required(VERSION 2.8) 2 | project( H264Decoder) 3 | 4 | find_package( OpenCV REQUIRED ) 5 | 6 | set(CMAKE_CXX_STANDARD 11) 7 | set(files 8 | # H264Decoder_jni.cpp 9 | H264Decoder.cpp H264Decoder.h 10 | sample.cpp #for sample 11 | ) 12 | 13 | include_directories( 14 | ${OpenCV_INCLUDE_DIRS} 15 | /home/curi/java/jdk1.8.0_131/include 16 | /home/curi/java/jdk1.8.0_131/include/linux 17 | /usr/include/libavcodec 18 | /usr/include/libavformat 19 | /usr/include/libswscale 20 | ) 21 | #add_library(H264Decoder SHARED ${files}) for build a .so file 22 | add_executable(H264Decoder ${files}) 23 | target_link_libraries(H264Decoder 24 | ${OpenCV_LIBS} 25 | /usr/lib/x86_64-linux-gnu/libavcodec.so 26 | /usr/lib/x86_64-linux-gnu/libavformat.so 27 | /usr/lib/x86_64-linux-gnu/libswscale.so 28 | /usr/lib/x86_64-linux-gnu/libavutil.so 29 | ) 30 | 31 | 32 | 33 | 34 | 35 | 36 | -------------------------------------------------------------------------------- /H264Decoder.cpp: -------------------------------------------------------------------------------- 1 | // 2 | // Created by curi on 17-6-28. 3 | // 4 | 5 | #include "H264Decoder.h" 6 | 7 | 8 | void H264Decoder::init() { 9 | 10 | matReady = false; 11 | 12 | avcodec_register_all(); 13 | av_init_packet(&avpkt); 14 | 15 | codec = avcodec_find_decoder(AV_CODEC_ID_H264); 16 | if (!codec) { 17 | fprintf(stderr, "Codec not found\n"); 18 | exit(1); 19 | } 20 | c = avcodec_alloc_context3(codec); 21 | if (!c) { 22 | fprintf(stderr, "Could not allocate video codec context\n"); 23 | exit(1); 24 | } 25 | 26 | if (avcodec_open2(c, codec, NULL) < 0) { 27 | fprintf(stderr, "Could not open codec\n"); 28 | exit(1); 29 | } 30 | 31 | frame = av_frame_alloc(); 32 | if (!frame) { 33 | fprintf(stderr, "Could not allocate video frame\n"); 34 | exit(1); 35 | } 36 | 37 | frame_count = 0; 38 | 39 | //存储解码后转换的RGB数据 40 | pFrameBGR = av_frame_alloc(); 41 | 42 | 43 | } 44 | 45 | void H264Decoder::decode(unsigned char *inputbuf, size_t size){ 46 | 47 | avpkt.size = size; 48 | if(avpkt.size == 0) 49 | return; 50 | 51 | avpkt.data = inputbuf; 52 | 53 | int len, got_frame; 54 | 55 | 56 | len = avcodec_decode_video2(c, frame, &got_frame, &avpkt); 57 | 58 | if (len < 0) { 59 | matReady = false; 60 | fprintf(stderr, "Error while decoding frame %d\n", frame_count); 61 | frame_count++; 62 | 63 | return ; 64 | } 65 | if(out_buffer == nullptr){ 66 | BGRsize = avpicture_get_size(AV_PIX_FMT_BGR24, c->width, 67 | c->height); 68 | out_buffer = (uint8_t *) av_malloc(BGRsize); 69 | avpicture_fill((AVPicture *) pFrameBGR, out_buffer, AV_PIX_FMT_BGR24, 70 | c->width, c->height); 71 | 72 | img_convert_ctx = 73 | sws_getContext(c->width, c->height, c->pix_fmt, 74 | c->width, c->height, AV_PIX_FMT_BGR24, SWS_BICUBIC, NULL, NULL, 75 | NULL); 76 | pCvMat.create(cv::Size(c->width, c->height), CV_8UC3); 77 | 78 | } 79 | if (got_frame) { 80 | matReady = true; 81 | sws_scale(img_convert_ctx, (const uint8_t *const *)frame->data, 82 | frame->linesize, 0, c->height, pFrameBGR->data, pFrameBGR->linesize); 83 | 84 | memcpy(pCvMat.data, out_buffer, BGRsize); 85 | 86 | // printf("decoding frame: %d\n",frame_count); 87 | frame_count++; 88 | } 89 | else{ 90 | matReady = false; 91 | } 92 | if (avpkt.data) { 93 | avpkt.size -= len; 94 | avpkt.data += len; 95 | } 96 | 97 | 98 | 99 | 100 | } 101 | 102 | void H264Decoder::play() { 103 | if(matReady){ 104 | cv::imshow("decode",pCvMat); 105 | cv::waitKey(1); 106 | } 107 | } 108 | 109 | H264Decoder::H264Decoder() { 110 | init(); 111 | } 112 | 113 | cv::Mat H264Decoder::getMat() { 114 | if(matReady){ 115 | return pCvMat; 116 | } 117 | else{ 118 | return cv::Mat(); 119 | } 120 | } 121 | 122 | 123 | 124 | -------------------------------------------------------------------------------- /H264Decoder.h: -------------------------------------------------------------------------------- 1 | // 2 | // Created by curi on 17-6-28. 3 | // 4 | 5 | #ifndef OPENCVIDEO_MASTER_H264DECODER_H 6 | #define OPENCVIDEO_MASTER_H264DECODER_H 7 | 8 | //#define __STDC_CONSTANT_MACROS 9 | #include 10 | #include 11 | // Opencv 12 | #include 13 | #include 14 | #include 15 | #include 16 | #include 17 | 18 | extern "C" { 19 | //#include "libavutil/avutil.h" 20 | #include "libavcodec/avcodec.h" 21 | #include "libavformat/avformat.h" 22 | //新版里的图像转换结构需要引入的头文件 23 | #include "libswscale/swscale.h" 24 | }; 25 | 26 | #if LIBAVCODEC_VERSION_INT < AV_VERSION_INT(55,28,1) 27 | #define av_frame_alloc avcodec_alloc_frame 28 | #endif 29 | 30 | 31 | class H264Decoder { 32 | 33 | public : 34 | 35 | H264Decoder(); 36 | // ~H264Decoder(); TODO 37 | void init(); 38 | void decode(unsigned char *inputbuf, size_t size); 39 | void play(); 40 | cv::Mat getMat(); 41 | 42 | private: 43 | 44 | const AVCodec *codec; 45 | AVCodecContext *c = nullptr; 46 | int frame_count; 47 | AVFrame *frame; 48 | AVPacket avpkt; 49 | AVFrame *pFrameBGR; 50 | 51 | int BGRsize; 52 | uint8_t *out_buffer = nullptr; 53 | 54 | struct SwsContext *img_convert_ctx; 55 | cv::Mat pCvMat; 56 | bool matReady; 57 | 58 | }; 59 | 60 | #endif //OPENCVIDEO_MASTER_H264DECODER_H 61 | -------------------------------------------------------------------------------- /H264Decoder.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Created by curi on 17-6-29. 3 | */ 4 | public class H264Decoder { 5 | 6 | static{ 7 | System.loadLibrary("H264Decoder"); 8 | } 9 | 10 | private long Decoder; 11 | 12 | public H264Decoder(){ 13 | Decoder = nativeH264init(); 14 | } 15 | 16 | public byte[] decode(byte[] frm,int len){ 17 | return nativeH264decode(Decoder,frm,len); 18 | 19 | } 20 | 21 | private native long nativeH264init(); 22 | 23 | private native byte[] nativeH264decode(long thiz,byte[] frm,int len); 24 | 25 | 26 | } 27 | -------------------------------------------------------------------------------- /H264Decoder_jni.cpp: -------------------------------------------------------------------------------- 1 | /* DO NOT EDIT THIS FILE - it is machine generated */ 2 | #include 3 | /* Header for class H264Decoder */ 4 | #include "H264Decoder.h" 5 | 6 | 7 | #ifndef _Included_H264Decoder 8 | #define _Included_H264Decoder 9 | #ifdef __cplusplus 10 | extern "C" { 11 | #endif 12 | /* 13 | * Class: H264Decoder 14 | * Method: nativeH264init 15 | * Signature: ()J 16 | */ 17 | JNIEXPORT jlong JNICALL Java_H264Decoder_nativeH264init 18 | (JNIEnv *env, jobject){ 19 | jlong result = (jlong)new H264Decoder(); 20 | return result; 21 | } 22 | 23 | /* 24 | * Class: H264Decoder 25 | * Method: nativeH264decode 26 | * Signature: (J[BI)[B 27 | */ 28 | JNIEXPORT jbyteArray JNICALL Java_H264Decoder_nativeH264decode 29 | (JNIEnv *env, jobject jobj, jlong thiz, jbyteArray frm, jint jlen){ 30 | H264Decoder * decoder = ((H264Decoder*)thiz); 31 | int len = jlen;// jint is long in C++, 32 | jbyte* bBuffer = env->GetByteArrayElements(frm,NULL);//remeber to get element 33 | unsigned char * buf = (unsigned char *)bBuffer; 34 | 35 | decoder->decode(buf,len); 36 | cv::Mat result = decoder->getMat(); 37 | if(result.data!=nullptr){ 38 | 39 | int offset = sizeof(int)/sizeof(unsigned char); 40 | offset*=2; 41 | 42 | int matSize[2]= {result.rows,result.cols}; 43 | 44 | int size = result.total()*result.elemSize(); 45 | 46 | jbyteArray resultArray = env->NewByteArray(size+offset); 47 | jbyte* array = env->GetByteArrayElements(resultArray,JNI_FALSE); 48 | 49 | memcpy(array,matSize,sizeof(matSize)); 50 | memcpy(array+offset,result.data,size); 51 | 52 | env->ReleaseByteArrayElements(resultArray,array,JNI_FALSE); 53 | return resultArray; 54 | } 55 | else{ 56 | return nullptr; 57 | } 58 | } 59 | 60 | #ifdef __cplusplus 61 | } 62 | #endif 63 | #endif 64 | -------------------------------------------------------------------------------- /readme.md: -------------------------------------------------------------------------------- 1 | - A H264decoder base on ffmpeg 2 | - please config your ffpmeg path in CMakeList.txt 3 | 4 | -------------------------------------------------------------------------------- /sample.cpp: -------------------------------------------------------------------------------- 1 | #include "H264Decoder.h" 2 | #include 3 | 4 | unsigned char buf[700000];//should be max enough 5 | 6 | int main() { 7 | 8 | H264Decoder decoder; 9 | for (int j = 1; j < 197; j++) { 10 | std::ifstream fin("/home/curi/hiro/ibotn/server/ws_streaming_server_java/264rawFrame/outputFrame" 11 | + std::to_string(j), std::ios_base::binary); 12 | fin.seekg(0, std::ios::end); 13 | int len = fin.tellg(); 14 | fin.seekg(0, std::ios::beg); 15 | 16 | fin.read((char *) buf, len); 17 | decoder.decode(buf, len); 18 | decoder.play(); 19 | } 20 | } 21 | 22 | --------------------------------------------------------------------------------