2017-07-25 3 views
0

Mein Bildformat ist "YUV422_8_UYVY", die AV_PIX_FMT_UYVY422 Format gepackt, ich bin versucht es in Planar "AV_PIX_FMT_YUVJ422P" zu konvertieren, aber nicht in der Lage erfolgreich zu sein noch, unten, ist der Code, an dem ich arbeite.Packet (AV_PIX_FMT_UYVY422) zu Planar (AV_PIX_FMT_YUVJ422P) Formatkonvertierung

Fehlermeldung: [swscaler @ 004b3fa0] deprecetd Pixelformat verwendet, stellen Sie sicher, dass Sie festgelegt haben Range richtig

sich ergebende Bild (Datei) mit 0 k Größe

was wäre der letztes Argument von av_image_alloc() zur Umwandlung wie 16,32 etc

mein Ziel Paket yuv Bild in planaren YUV-Format

static AVCodecContext  *pCodecCtx; 
    static AVFormatContext  *pFormatCtx; 
    static AVCodec    *pCodec; 
    static AVOutputFormat*  fmt; 
    static AVFrame    *RawPic; 
    static AVFrame    *ScalePic; 
    static AVPacket   pkt; 
    static AVStream*   video_st; 
    static FILE    *file; 
    static struct SwsContext *sws_ctx; 

enum AVPixelFormat  src_pix_fmt  = AV_PIX_FMT_UYVY422; 
enum AVPixelFormat  dst_pix_fmt  = AV_PIX_FMT_YUVJ422P; 

int main() { 

     FILE *in_file   = NULL;    //packed Source 
     FILE *out_file   = NULL;    //planar output 
     int  in_width  = 2448;    //YUV's width 
     int  in_height  = 2050;    //YUV's heigh 

     int  out_width  = 2448;    //YUV's width 
     int  out_height  = 2050;    //YUV's heigh 


     unsigned long int  ret; 

     in_file = fopen("c:\\yuv422_8_uyvy.yuv","rb");  //Source Input File 
     if(in_file == NULL) { printf("\n\tinput File Opening error...!!"); exit(1); } 



     out_file = fopen("d:\\test_Planar.yuv", "wb");    //Source Input File 
     if(out_file == NULL) { printf("\n\toutput File Opening error...!!"); exit(1); } 
     else     { printf("\n\tOutput File Created...!!"); } 


//------Loads the whole database of available codecs and formats------ 
     av_register_all(); 
     printf("\t\n\tCodac database Loaded...\n"); 

//------Contex Variable assignment-------------------------------- 
     pFormatCtx    = avformat_alloc_context();  
     fmt      = NULL; 
     fmt      = av_guess_format("mjpeg",NULL,NULL); 
     pFormatCtx->oformat  = fmt; 


     video_st = avformat_new_stream(pFormatCtx, 0); if (video_st==NULL) return -1; 

     pCodecCtx    = video_st->codec; 
     pCodecCtx->codec_id  = fmt->video_codec; 
     pCodecCtx->codec_type = AVMEDIA_TYPE_VIDEO; 
     pCodecCtx->pix_fmt  = src_pix_fmt; 
     printf("\t\n\tContex Variable assigned...\n"); 

//------Allocate Source Image Buffer-------------------------------- 
     AVFrame *RawPic = av_frame_alloc(); 
     if(!RawPic) { printf("\nCould not allocate Raw Image frame\n"); exit(1);} 
     RawPic->format = pCodecCtx->pix_fmt; 
     RawPic->width = in_width; 
     RawPic->height = in_height;  
     ret = av_image_alloc(RawPic->data,RawPic->linesize,in_width,in_height,src_pix_fmt, 16); 
     if(ret < 0) { printf("\nCould not allocate raw picture buffer\n"); exit(1);} 
     printf("\n\tAllocate Source Image Buffer"); 

//------Allocate Desitnation Image Buffer------------------- 
     AVFrame *ScalePic = av_frame_alloc(); 
     if(!ScalePic) { printf("\nCould not allocate Scale Image frame\n"); exit(1);}  
     ScalePic->format = pCodecCtx->pix_fmt; 
     ScalePic->width  = out_width; 
     ScalePic->height = out_height;  
     ret = av_image_alloc(ScalePic->data,ScalePic->linesize,out_width,out_height,dst_pix_fmt, 32); 
     if(ret < 0) { printf("\nCould not allocate Scale picture buffer\n"); exit(1);} 
     dst_bufsize = ret; 
     printf("\n\tAllocate Destination Image Buffer"); 


//------Create scaling context------------------------------sws_getContex 
     printf("\t\n\tCreating Scaling context..[sws_getContext]\n"); 

     sws_ctx = sws_getContext(in_width,  in_height,  src_pix_fmt, 
            out_width,  out_height,  dst_pix_fmt, 
            SWS_BICUBIC, NULL, NULL, NULL); 
     if(!sws_ctx) { printf("\nContext Error..\n"); } 
     printf("\t\n\tScaling context...Created\n"); 


//------Create scaling context---OR CONVERTED TO DESTINATION FORMAT--  
     sws_scale(sws_ctx, RawPic->data, RawPic->linesize, 0, in_height, ScalePic->data, ScalePic->linesize);  
     printf("\t\n\tCreating Scaling context...sws_scale...done\n"); 

     int num_bytes = avpicture_get_size(src_pix_fmt,in_width,in_height); 
     uint8_t* ScalePic_Buffer = (uint8_t *)av_malloc(num_bytes*sizeof(int8_t));  
     avpicture_fill((AVPicture*)ScalePic,ScalePic_Buffer,AV_PIX_FMT_YUVJ422P,out_width,out_height); 


//-----Write Scale Image to outputfile---------------------------- 
     fwrite(ScalePic->data,1,dst_bufsize,out_file); 



//---Release all memory and close file---------------------------------- 
    fclose(in_file); 
    fclose(out_file); 
    avcodec_close(pCodecCtx); 
    av_free(pCodecCtx); 
    av_freep(&RawPic->data[0]); 
    av_frame_free(&RawPic); 
    av_freep(&ScalePic->data[0]); 
    av_frame_free(&ScalePic); 
    av_frame_free(&RawPic); 

    printf("\n\n"); 
    system("PAUSE"); 
    exit(1); 

} 

Antwort

0

Es gibt Haufen Fehler konvertieren Sie in Ihrem Codes gemacht. Ich kann Ihnen ein paar Hinweise geben, aber Sie sollten versuchen, alles zu erledigen, damit es endlich funktioniert.

  1. dst_bufsize = ret; sollte, sonst entfernt werden dst_bufsize 0. immer
  2. Der erste Parameter von fwrite sollte statt ScalePic->data so etwas wie ScalePic->data[0] sein wird.
  3. Sie rufen nie fread auf in_file, das bedeutet, dass Sie nie in der Quelldatei lesen.
  4. Verwenden Sie nicht avpicture_fill gegen ScalePic, es überschreibt den Inhalt in ScalePic.

Für Ihre Fragen:

  1. Der letzte Parameter von av_image_alloc ist align - der Wert für Puffergröße Ausrichtung zu verwenden.
  2. [swscaler @ 004b3fa0] deprecetd pixel format used, make sure you did set range correctly Dies ist keine Fehlermeldung, sondern eine Warnung, die den Konvertierungsfehler nicht verursacht hat.
+0

warum wir Größe Ausrichtung Puffer benötigen? und wenn erforderlich wie diesen Wert zu bestimmen, wie ich AV_PIX_FMT_UYVY422 in AV_PIX_FMT_YUVJ422P oder ein anderes Format konvertieren möchten, was wird dieser Wert sein? und was dieses zweite Argument linesize? – user3743908

+0

Ich habe folgende Änderungen vorgenommen und beginne Bild 1 zu erhalten> Lies die Bilddatei und lege sie in RawPic-> Daten [0], 2> dst_buffer = ret bleibe intakt, aber vorher führe sws_scale aus damit das Bild (konvertierte Bilddaten) geladen wird In ScalePic-> data [0], 3> weisen Sie auch Bildpuffer für ein Bild ähnlich wie bei ScalePic zu und laden in_file in diesen Puffer. NOCH NICHT WISSEN über das letzte Argument BUFFER SIZE ALIGNMENT – user3743908

+0

https://stackoverflow.com/questions/35678041/what-islinesize-alignment-meaning – cycycyc

0

ich bin erfolgreich Lage

YUV verpackt Bild in Planar-Format unter-Code zu konvertieren
FILE *in_file = NULL; //fopen("myHexFile.yuv","rb"); input PACKED 
FILE *out_file = NULL; //Output File Planar format 

int in_width  = 2448;    //YUV's width 
int in_height  = 2050;    //YUV's heigh 
int out_width  = 2448;    //YUV's width 
int out_height  = 2050;    //YUV's heigh 

int  in_linesize[4]; 
int  out_linesize[4]; 
uint8_t  *in_data[4], *out_data[4]; 

unsigned long int  out_bufsize,in_bufsize; 

in_file = fopen("myHexFile.yuv","rb"); //This is YUV422-UYVY Input packed image 

if(in_file == NULL) 
{ 
this->Print2TextBox1(L"Input File Opening error...!"); 
exit(1); 
} 

out_file = fopen("d:\\myHexFile_Planar.yuv", "wb");  //Source Input File 
if(out_file == NULL) 
{  
this->Print2TextBox1(L"toutput File Opening error...!!"); 
exit(1); 
} 

else { this->Print2TextBox1(L"Output File Created...!!\n"); }  

//-Loads the whole database of available codecs and formats------- 
    av_register_all(); 
    this->Print2TextBox1(L"Codac database Loaded...\n"); 

//---Create scaling context------------------------sws_getContex 
this->Print2TextBox1(L"Creating Scaling context..\n"); 

sws_ctx = sws_getContext(in_width, in_height, src_pix_fmt, 
          out_width,out_height,dst_pix_fmt, 
          SWS_BICUBIC, NULL, NULL, NULL); 

if(!sws_ctx) { this->Print2TextBox1(L"Context Error..\n"); } 



    //--Allocate Source Image Buffer-------------------------- 
    this->Print2TextBox1(L"Allocate Source Image Buffer...\n"); 
AVFrame *RawPic = av_frame_alloc(); 
if(!RawPic) 
    {  
    this->Print2TextBox1(L"Could not allocate Raw Image frame\n"); 
    exit(1); 
    } 



RawPic->format = src_pix_fmt; 
RawPic->width = in_width; 
RawPic->height = in_height; 

int num_bytes1 = avpicture_get_size(src_pix_fmt,in_width,in_height); 
uint8_t* RawPic_Buffer = (uint8_t*)av_malloc(num_bytes1*sizeof(int8_t)); 
ret =av_image_alloc(RawPic->data,in_linesize,in_width,in_height,src_pix_fmt, 1); 

if(ret < 0) 
{ 
this->Print2TextBox1(L"Could not allocate raw picture buffer\n"); 
exit(1); 
} 

in_bufsize = ret; 
//------Reading Input Image and Store in RawPic->Data Pointer--- 
fread(RawPic->data[0],1,in_bufsize,in_file); 

//----Allocate Desitnation Image Buffer------------------- 
this->Print2TextBox1(L"Allocate Destination Image Buffer...\n"); 

AVFrame *ScalePic = av_frame_alloc(); 

if(!ScalePic) 
{ 
this->Print2TextBox1(L"Could not allocate Scale Image frame\n");  
exit(1); 
}  

ScalePic->format = dst_pix_fmt;//pCodecCtx->pix_fmt; 
ScalePic->width  = out_width; 
ScalePic->height = out_height;  
int num_bytes2 = avpicture_get_size(dst_pix_fmt,out_width,out_height); 
uint8_t* ScalePic_Buffer = (uint8_t *)av_malloc(num_bytes2*sizeof(int8_t)); 

ret = av_image_alloc(ScalePic->data,out_linesize,out_width,out_height,dst_pix_fmt, 1); //16 

if(ret < 0) { this->Print2TextBox1(L"Could not allocate Scale picture buffer\n"); exit(1);} 
out_bufsize = ret; 

//-Create scaling context-OR CONVERTED TO DESTINATION FORMAT-----sws_scale 
this->Print2TextBox1(L"Creating Scaling context...sws_scale\n"); 


sws_scale(sws_ctx, RawPic->data, in_linesize, 0, ScalePic->height, ScalePic->data, out_linesize); 

//-----Write Scale Image to outputfile- 

    this->Print2TextBox1(L"Write Scale Image to outputfile..\n"); 
    fwrite(ScalePic->data[0],1,out_bufsize,out_file); 

//---Release all memory and close file-- 
       fclose(in_file); 
       fclose(out_file); 

     av_freep(&RawPic->data[0]); 
     av_freep(&ScalePic->data[0]); 

     av_frame_free(&ScalePic); 
     av_frame_free(&RawPic);