[FFMPEG]h264的YUV420数据的存储

作者: _小老虎_ | 来源:发表于2018-12-13 20:26 被阅读55次

前面文章有人问我解码出来的264如何播放,一般用OPENGLES去渲染

  • 这里只给关键部分代码, 从最近做的项目里面摘出来的。
    这部分内容推荐看一本书《音视频进阶开发指南Android和iOS》
    c++

这里我定义了一个snode结构体来存储YUV数据,这个自己也可以写;

还有一个经常被忽略的问题:记住处理内存泄露的问题,如果常年不写c++的话可能会忽视掉这个问题。 用PS检测内存泄露情况,跑上个十来八个小时的看下内存变换情况; 但是如果你常年写java,php,python操作系统课本不扎实的话就得回头好好补补了,代码风骚的背后需要理论功底去突破瓶颈,不需要背名词,但要记原理

        if (open_ret1 >= 0 ) {
                avcodec_decode_video2(rtspVideoCodecContext, v_frame, &got_picture, read_packet);
                printf("1\n");
                if (got_picture) {
                    // Init And Define YUV INFO
                    /* 
                    snode->luma = new unsigned char[width*height];
                    snode->chromaB = new unsigned char[width*height/4];
                    snode->chromaR = new unsigned char[width*height/4];
                    */

                    printf("alloc ok\n");
                    unsigned int height = rtspVideoCodecContext->height;
                    unsigned int width = rtspVideoCodecContext->width;
                    printf("decode video ok:%d,%d\n",width,height);

                    if (snode->luma != NULL) delete []snode->luma;
                    if (snode->chromaB != NULL) delete []snode->chromaB;
                    if (snode->chromaR != NULL) delete []snode->chromaR;
                    snode->luma = NULL;
                    snode->chromaB = NULL; 
                    snode->chromaR = NULL; 

                    snode->luma = new char[width*height];
                    snode->chromaB = new char[width*height/4];
                    snode->chromaR = new char[width*height/4];

                    int i;
                    for (i = 0; i<height; i++) {
                        memcpy(snode->luma + width*i, v_frame->data[0] + i * v_frame->linesize[0], width);
                        //memcpy(snode->luma + a, v_frame->data[0] + i * v_frame->linesize[0], width);
                    }
                    for (i = 0; i<height / 2; i++) {
                        memcpy(snode->chromaB + width/2*i, v_frame->data[1] + i * v_frame->linesize[1], width / 2);
                        //memcpy(snode->chromaB + a, v_frame->data[1] + i * v_frame->linesize[1], width / 2);
                    }
                    for (i = 0; i<height / 2; i++) {
                        memcpy(snode->chromaR + width/2*i, v_frame->data[2] + i * v_frame->linesize[2], width / 2);
                        //memcpy(snode->chromaR + a, v_frame->data[2] + i * v_frame->linesize[2], width / 2);
                    }

                    snode->luma_length = width*height;
                    snode->chromaB_length = (width*height)/4;
                    snode->chromaR_length = (width*height)/4;

                    snode->pts = read_packet->pts;
                    snode->width = width;
                    snode->height = height;

                    printf("decode video ok:%d,%d\n%d,%d,%d\n",width,height,snode->luma_length,snode->chromaB_length,snode->chromaR_length);

                    printf("set data ok\n");
                    printf("===>%d\n",snode->pts);
                    //exit(0);
                } // if got_pic
            }

相关文章

网友评论

    本文标题:[FFMPEG]h264的YUV420数据的存储

    本文链接:https://www.haomeiwen.com/subject/mpxihqtx.html