zoukankan      html  css  js  c++  java
  • FFMPEG内存操作(二)从内存中读取数及数据格式的转换

    相关博客列表: 

       FFMPEG内存操作(一) avio_reading.c 回调读取数据到内存解析 

        FFMPEG内存操作(二)从内存中读取数及数据格式的转换 

        FFmpeg内存操作(三)内存转码器

        在雷神的《最简单的基于FFmpeg的内存读写例子(内存播放器)》中,它是设计回调函数从输入文件中读取数据。与FFMPEG 官方给出的avio_reading.c不同的是,雷神给的例子是当需要数据的时候,回调函数才去从输入文件读取数据,而avio_reading.c 则是直接全部数据读取到内存中待后面处理。

        在我的这个实例中,我是将读取到的输入文件解码成YUV420P数据格式。同时可以通过设置不同的数据格式和尺寸实现输出图像的拉伸缩小或是数据格式的装换。

    [objc] view plain copy
     
     print?
    1. /*=============================================================================    
    2. #     FileName: ffmpeg_mem_read.c    
    3. #         Desc: an example of ffmpeg read from memory 
    4. #       Author: licaibiao    
    5. #   LastChange: 2017-03-21     
    6. =============================================================================*/    
    7. #include <stdio.h>  
    8.   
    9. #define __STDC_CONSTANT_MACROS  
    10. #include "avcodec.h"  
    11. #include "avformat.h"  
    12. #include "swscale.h"  
    13.   
    14. //#define QUARTER_SHOW     
    15.   
    16. charchar *input_name = "cuc60anniversary_start.ts";  
    17.   
    18. FILEFILE *fp_open = NULL;  
    19.   
    20. int read_buffer(voidvoid *opaque, uint8_t *buf, int buf_size){  
    21.     if(!feof(fp_open)){  
    22.         int true_size=fread(buf,1,buf_size,fp_open);  
    23.         return true_size;  
    24.     }else{  
    25.         return -1;  
    26.     }  
    27. }  
    28.   
    29. int main(int argc, char* argv[])  
    30. {  
    31.     AVFormatContext *pFormatCtx;  
    32.     AVCodecContext  *pCodecCtx;  
    33.     AVCodec         *pCodec;  
    34.     AVIOContext     *avio;  
    35.     AVFrame         *pFrame;  
    36.     AVFrame         *pFrameYUV;  
    37.     AVPacket        *packet;  
    38.       
    39.     struct SwsContext *img_convert_ctx;  
    40.   
    41.     int             videoindex;  
    42.     int             i;  
    43.     int             ret;  
    44.     int             got_picture;  
    45.     int             y_size;  
    46.     unsigned char   *aviobuffer;  
    47.     FILE            *fp_yuv;  
    48.   
    49.   
    50.     //fp_open = fopen(argv[1],"rb+");  
    51.     fp_open = fopen(input_name, "rb+");  
    52.     fp_yuv  = fopen("output.yuv", "wb+");       
    53.   
    54.     av_register_all();  
    55.     pFormatCtx = avformat_alloc_context();  
    56.   
    57.     aviobuffer=(unsigned charchar *)av_malloc(32768);  
    58.     avio = avio_alloc_context(aviobuffer, 32768,0,NULL,read_buffer,NULL,NULL);  
    59.   
    60.     /* Open an input stream and read the header. */  
    61.     pFormatCtx->pb = avio;  
    62.     if(avformat_open_input(&pFormatCtx,NULL,NULL,NULL)!=0){  
    63.         printf("Couldn't open input stream. ");  
    64.         return -1;  
    65.     }  
    66.   
    67.     /* Read packets of a media file to get stream information. */  
    68.     if(avformat_find_stream_info(pFormatCtx,NULL)<0){  
    69.         printf("Couldn't find stream information. ");  
    70.         return -1;  
    71.     }  
    72.       
    73.     videoindex = -1;  
    74.     for(i=0; i<pFormatCtx->nb_streams; i++) {  
    75.         if(pFormatCtx->streams[i]->codec->codec_type==AVMEDIA_TYPE_VIDEO){  
    76.             videoindex=i;  
    77.             break;  
    78.         }  
    79.     }   
    80.       
    81.     if(videoindex==-1){  
    82.         printf("Didn't find a video stream. ");  
    83.         return -1;  
    84.     }  
    85.   
    86.     av_dump_format(pFormatCtx, 0, input_name, 0);  
    87.   
    88.     /* Find a registered decoder with a matching codec ID */  
    89.     pCodecCtx = pFormatCtx->streams[videoindex]->codec;  
    90.     pCodec    = avcodec_find_decoder(pCodecCtx->codec_id);     
    91.     if(pCodec==NULL){  
    92.         printf("Codec not found. ");  
    93.         return -1;  
    94.     }  
    95.   
    96.     /* Initialize the AVCodecContext to use the given AVCodec */  
    97.     if(avcodec_open2(pCodecCtx, pCodec,NULL)<0){  
    98.         printf("Could not open codec. ");  
    99.         return -1;  
    100.     }  
    101.       
    102.     pFrame    = av_frame_alloc();  
    103.     pFrameYUV = av_frame_alloc();  
    104.     uint8_t *out_buffer=(uint8_t *)av_malloc(avpicture_get_size(AV_PIX_FMT_YUV420P, pCodecCtx->width, pCodecCtx->height));  
    105.     avpicture_fill((AVPicture *)pFrameYUV, out_buffer, AV_PIX_FMT_YUV420P, pCodecCtx->width, pCodecCtx->height);  
    106.   
    107.     /*  Allocate and return an SwsContext. */  
    108.     /* srcW:源图像的宽 
    109.      * srcH:源图像的高 
    110.      * srcFormat:源图像的像素格式 
    111.      * dstW:目标图像的宽 
    112.      * dstH:目标图像的高 
    113.      * dstFormat:目标图像的像素格式 
    114.      * flags:设定图像拉伸使用的算法 
    115.     */  
    116. #ifndef QUARTER_SHOW  
    117.     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);   
    118. #else  
    119.     img_convert_ctx = sws_getContext(pCodecCtx->width, pCodecCtx->height, pCodecCtx->pix_fmt, pCodecCtx->width/2, pCodecCtx->height/2, AV_PIX_FMT_YUV420P, SWS_BICUBIC, NULL, NULL, NULL);   
    120.     printf("out frame  width = %d, height = %d  ", pCodecCtx->width/2,pCodecCtx->height/2);  
    121. #endif  
    122.     packet = (AVPacket *)av_malloc(sizeof(AVPacket));  
    123.     while(av_read_frame(pFormatCtx, packet) >= 0){  
    124.         if(packet->stream_index == videoindex){  
    125.             ret = avcodec_decode_video2(pCodecCtx, pFrame, &got_picture, packet);  
    126.             if(ret < 0){  
    127.                 printf("Decode Error. ");  
    128.                 return -1;  
    129.             }  
    130.             if(got_picture){  
    131.                 /* Scale the image slice in srcSlice and put the resulting scaled slice in the image in dst. 图像处理函数 */  
    132.                 /* c             the scaling context previously created with  sws_getContext() 
    133.                  * srcSlice      the array containing the pointers to the planes of the source slice 
    134.                  * srcStride     the array containing the strides for each plane of 
    135.                  * srcSliceY     the position in the source image of the slice to process, that is the number (counted starting from 
    136.                                  zero) in the image of the first row of the slice 输入图像数据的第多少列开始逐行扫描,通常设为0 
    137.                  * srcSliceH     the height of the source slice, that is the number of rows in the slice 为需要扫描多少行,通常为输入图像数据的高度 
    138.                  * dst           the array containing the pointers to the planes of the destination image 
    139.                  * dstStride     the array containing the strides for each plane of the destination image 
    140.                  */    
    141.                 sws_scale(img_convert_ctx, (const uint8_t* const*)pFrame->data, pFrame->linesize, 0, pCodecCtx->height, pFrameYUV->data, pFrameYUV->linesize);  
    142.                  
    143. #ifndef QUARTER_SHOW  
    144.                // printf("pFrameYUV->height   = %d  ",pFrameYUV->height);  
    145.                // printf("pFrameYUV->width    = %d  ", pFrameYUV->width);  
    146.                // printf("pFrameYUV->pkt_size = %d  ",pFrameYUV->pkt_size);  
    147.   
    148.                 y_size = pCodecCtx->width*pCodecCtx->height;   
    149.                 fwrite(pFrameYUV->data[0],1,y_size,fp_yuv);    //Y   
    150.                 fwrite(pFrameYUV->data[1],1,y_size/4,fp_yuv);  //U  
    151.                 fwrite(pFrameYUV->data[2],1,y_size/4,fp_yuv);  //V  
    152. #else  
    153.                 for(i=0; i<pCodecCtx->height/2; i++){  
    154.                     fwrite(pFrameYUV->data[0]+pCodecCtx->width * i ,1,pCodecCtx->width/2,fp_yuv);   
    155.                 }  
    156.                 for(i=0; i<pCodecCtx->height/2; i = i + 2){  
    157.                     fwrite(pFrameYUV->data[1]+pCodecCtx->width * i/4 ,1,pCodecCtx->width/4,fp_yuv);  
    158.                 }  
    159.   
    160.                 for(i=0; i<pCodecCtx->height/2; i = i + 2 ){               
    161.                     fwrite(pFrameYUV->data[2]+pCodecCtx->width * i/4 ,1,pCodecCtx->width/4,fp_yuv);   
    162.                 }      
    163. #endif               
    164.                       
    165.             }  
    166.         }  
    167.         av_free_packet(packet);  
    168.     }  
    169.     sws_freeContext(img_convert_ctx);  
    170.   
    171.     fclose(fp_yuv);  
    172.     fclose(fp_open);  
    173.     av_free(out_buffer);  
    174.     av_free(pFrameYUV);  
    175.     avcodec_close(pCodecCtx);  
    176.     avformat_close_input(&pFormatCtx);  
    177.   
    178.     return 0;  
    179. }  

        正常操作是将输入文件解码成YUV数据。

        当我们对sws_getContext  设置不同参数的时候,我们可以得到不同的输出数据。在这里需要注意几点:

        (1)out_buffer 的大小要跟着sws_getContext  参数的设置而改变,如果out_buffer分配得比输出数据小,会出现内存溢出问题。

        (2)sws_scale 函数中,我们一般设置从输入数据的第0行开始扫描,扫描的高度(也就是行数)一般是输入数据的高度。

        (3)得到的YUV数据是储存在pFrameYUV->data 的三个分量里,需要分开读取。

    举例:

        在sws_getContext 中设置输出格式的宽和高都是输入格式的一半。正常的显示应该是图像变为了原来的1/4大小。如果我们还是按原图尺寸提取输出数据:

    [objc] view plain copy
     
     print?
    1.  y_size = pCodecCtx->width*pCodecCtx->height;   
    2. fwrite(pFrameYUV->data[0],1,y_size,fp_yuv);    //Y   
    3. fwrite(pFrameYUV->data[1],1,y_size/4,fp_yuv);  //U  
    4. fwrite(pFrameYUV->data[2],1,y_size/4,fp_yuv);  //V  

    得到的图像将会是:

        如上图,图像显示是正常的,但同时也提取了很多的无用数据,实际图像并没有缩小到原来的1/4

        所以我们需要自己对输出数据再重组:

    [objc] view plain copy
     
     print?
    1. for(i=0; i<pCodecCtx->height/2; i++){  
    2.     fwrite(pFrameYUV->data[0]+pCodecCtx->width * i ,1,pCodecCtx->width/2,fp_yuv);   
    3. }  
    4. for(i=0; i<pCodecCtx->height/2; i = i + 2){  
    5.     fwrite(pFrameYUV->data[1]+pCodecCtx->width * i/4 ,1,pCodecCtx->width/4,fp_yuv);  
    6. }  
    7.   
    8. for(i=0; i<pCodecCtx->height/2; i = i + 2 ){               
    9.     fwrite(pFrameYUV->data[2]+pCodecCtx->width * i/4 ,1,pCodecCtx->width/4,fp_yuv);   
    10. }   

        分别重新提取了Y U V 三个分量上的数据,得到图像如下:

        在读取输出数据的时候,如果发现读取的输出数据显示有问题,最好是使用UltraEdit 工具对某帧数据进行分析,先确认输出的一帧数据是否正常,然后才进行视频格式的转换。比如出现下面的这种情况

        从图像中大概能分析出来,Y分量数据是正常的,图像右半边的UV分量可能丢失。使用UltraEdit查看原始数据,如下图。我们可以看到从地址0xe150开始有一段是全0的数据,因此我们可以到我们的程序中去检查是否UV分量读取错误。

        总结:我们直接使用sws_getContext和sws_scale 可以直接对图像进行拉伸和缩放,同时可以进行数据格式的转换,只需要设置sws_getContext 就可以了,非常简单。但是需要注意,输出的数据需要我们自己重新组合,否则读取到的输出数据很有可能出错。

    from:http://blog.csdn.net/li_wen01/article/details/64904586

  • 相关阅读:
    sqlParameter的两种写法 以及存储过程还有sql语句(防注入)
    SqlServer2005 SQL Server 版本变更检查 警告
    禁用自带防火墙
    sql分页
    每个程序员都必须遵守的编程原则
    在PDA设备上安装SQL Server Compact
    Mcrosoft SQL Server 自定义函数
    程序员人生之路(转)
    在windows 7 上为 sqlserver 2008 启用远程访问
    在PDA设备上安装和部署 SQL Server Compac 3.5(官方版)
  • 原文地址:https://www.cnblogs.com/lidabo/p/7346264.html
Copyright © 2011-2022 走看看