FFmpeg on Android Output slide screen problem processing

Source: Internet
Author: User

FFmpeg a problem with a flower screen on some machines.


The original code is as follows:

while (Av_read_frame (Formatctx, &packet) >=0 &&!_stop && null!=window && binit) {//are This a packet from the video stream?if (packet.stream_index==videostream) {//Decode video Frameavcodec_decode_video2 ( Codecctx, Decodedframe, &framefinished, &packet);/Did we get a video frame?if (framefinished) {//Convert the I Mage from it native format to Rgbasws_scale (SWS_CTX, (uint8_t const * const *) decodedframe->data,decodedframe-> Linesize,0,codecctx->height,framergba->data,framergba->linesize); if (Packet.dts = = AV_NOPTS_VALUE & & Decodedframe->opaque && * (uint64_t*) Decodedframe->opaque! = Av_nopts_value) {pts = * (uint64_t *) decodedframe->opaque; LOGD ("Pst1:%d", pts);  else if (Packet.dts! = av_nopts_value) {pts = Packet.dts; LOGD ("Pst2:%d", pts);  else {pts = 0; LOGD ("Pst3:%d", pts); pts = av_q2d (codecctx->time_base) * 1000000.0 * i * 2;pts *= 1000;//logd ("Debug%d,%d,%f", pts, (Long) (Av_q2d (codecctx ->time_Base) * 1000000.0 * I * 2), av_q2d (codecctx->time_base)); if (0 = = Pts | | 0 = = basetime) {basetime = Av_gettime ()-pts; LOGD ("Basetime:%d", basetime);} Else{waittime = (Basetime + pts)-av_gettime (); LOGD ("WAITTIME:%d,%d", waittime,pts);} WaitTime = (av_q2d (codecctx->time_base) * 1000.0-0.0) * 1000;IF (waittime>0) usleep (waitTime); if (!_stop) { Synchronized (Lockwindow) {if (!_stop && Null!=window) {//Lock the window Bufferif (Anativewindow_lock (Pwin, &windowbuffer, NULL) < 0) {LOGE ("Cannot lock Window");} else {//Draw the frame on buffer//logd ("Copy buffer%d:%d: %d ", width, height, width*height*rgb_size);//LOGD (" Window buffer:%d:%d:%d ", Windowbuffer.width, Windowbuffer.height,  Windowbuffer.stride); memcpy (windowbuffer.bits, buffer, Width * height * rgb_size);//unlock the window buffer and post it To Displayanativewindow_unlockandpost (Pwin);//count number of Frames++i;}}}}
After careful analysis, found that some of the resolution and can be normal display, feeling is caused by the width dislocation, analysis as follows:

org:176 *144 F
x2:352 288 O
x3:528 432 F
x4:704 576 O
x6:1056 * O


X1.1 193 158 F
X1.2 211 172 F
X1.5 264 216 F


X0.5 88 The F




X2?: 352 290 O
X2?: 352 O
X2?: 352 720 O
X4?: 704 720 O
X6?: 1056 720 O



---1312
1056
1184
1248 OK

Find resolution according to%64+32, feeling is caused by memory alignment, view Anativewindow_buffer as follows

typedef struct ANATIVEWINDOW_BUFFER {//The number of pixels that is    show horizontally.    int32_t width;    The number of pixels that is shown vertically.    int32_t height;    The number of *pixels* that a line in the buffer takes in    //memory.  This is >= width.    int32_t Stride;    The format of the buffer.  One of window_format_*    int32_t FORMAT;    The actual bits.    void* bits;        Do not touch.    uint32_t reserved[6];} Anativewindow_buffer;


Output stride and width of the log found, if the normal display is stride==width, through the comments can be seen to be caused by memory alignment problems, adjust the code:

if (Packet.stream_index==videostream) {//Decode video Frameavcodec_decode_video2 (Codecctx, Decodedframe, & Framefinished, &packet);/Do we get a video frame?if (framefinished) {//Convert the image from its native format t o Rgbasws_scale (sws_ctx, (uint8_t const * const *) decodedframe->data,decodedframe->linesize,0,codecctx-> Height,framergba->data,framergba->linesize); if (Packet.dts = = Av_nopts_value && decodedframe-> Opaque && * (uint64_t*) Decodedframe->opaque! = Av_nopts_value) {pts = * (uint64_t *) decodedframe->opaque; LOGD ("Pst1:%d", pts);  else if (Packet.dts! = av_nopts_value) {pts = Packet.dts; LOGD ("Pst2:%d", pts);  else {pts = 0; LOGD ("Pst3:%d", pts); pts = av_q2d (codecctx->time_base) * 1000000.0 * i * 2;pts *= 1000;//logd ("Debug%d,%d,%f", pts, (Long) (Av_q2d (codecctx ->time_base) * 1000000.0 * I * 2), av_q2d (codecctx->time_base)); if (0 = = Pts | | 0 = = basetime) {basetime = Av_gettime ( )-pts; LOGD ("Basetime:%d", basetime);} ElSe{waittime = (Basetime + pts)-av_gettime (); LOGD ("WAITTIME:%d,%d", waittime,pts);} WaitTime = (av_q2d (codecctx->time_base) * 1000.0-0.0) * 1000;IF (waittime>0) usleep (waitTime); if (!_stop) { Synchronized (Lockwindow) {if (!_stop && Null!=window) {//Lock the window Bufferif (Anativewindow_lock (Pwin, &windowbuffer, NULL) < 0) {LOGE ("Cannot lock Window");} else {//Draw the frame on buffer//logd ("Copy buffer%d:%d: %d ", width, height, width*height*rgb_size);//LOGD (" Window buffer:%d:%d:%d ", Windowbuffer.width, Windowbuffer.height, Windowbuffer.stride);//memcpy (windowbuffer.bits, buffer, Width * height * rgb_size); if (Windowbuffer.width >= Windowbuffer.stride) {memcpy (windowbuffer.bits, buffer, Width * height * rgb_size);} Else{//skip stride-width Skip padding partial memory for (int i=0;i
Through the line copy method, after jumping to face the memory of the aligned part,

Solve the problem,


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.