Weird code that seems to have enormous amounts of trial and error debugging. This doesn't look like Delphi/Lazarus but an enormous amount of trial-and-error work, and maybe simply using a different stream from back then with Delphi, and the program, old and crufty that it is reacting bad to it. I count multiple sets of opengl headers, copy and pasted bmp structures, UGH.
First and for all. You might not want to do these changes by hand since you use opengl for display. Some changes (like RGB swap and topdown) can be handled by OpenGL.
E.g. you might be avoid the whole need for byteswapping by changing this code
if Format = GL_RGBA then
gluBuild2DMipmaps(GL_TEXTURE_2D, GL_RGBA, Width, Height, GL_RGBA, GL_UNSIGNED_BYTE, pData)
else
gluBuild2DMipmaps(GL_TEXTURE_2D, 3, Width, Height, GL_RGB, GL_UNSIGNED_BYTE, pData);
to read GL_BGR and GL_BGRA. I don't know GLU though, I always used opengl directly, and not using mipmaps.
Some streams generate topdown code (first line is top line of the image), some bottom up (first line in memory is last line of image). This code:
glBegin(GL_QUADS);
glTexCoord2f(0, 0); glVertex3f(-1,-1,0);
glTexCoord2f(1, 0); glVertex3f(1,-1,0);
glTexCoord2f(1, 1); glVertex3f(1,1,0);
glTexCoord2f(0, 1); glVertex3f(-1,1,0);
glEnd;
probably governs drawing. negating some of the glvertex3f Y (second) coordinates might change the image topdown.
As to the image corruption (it is not just the above problems since I see much more loops than in the original which means something 3-byte vs 4-bite probably, or origin image having different dimensions than the target image (e.g. due to the sws_scale)
So I search for your code line and look at it:
SwapTB(pFrameYUV420P^.data[0],pData,codec_context^.width,codec_context^.height);
This copies from pFrameYUV420P^.data[0],pData.
Pdata is a rightly sized buffer:
GetMem(pData, codec_context^.width*codec_context^.height*4);
so probably not the problem.
So the problem seems to be the source. I also don't understand how "mybuffer" factors into it. Also its calculation is a bit weird
numBytes: = avpicture_get_size (AV_PIX_FMT_RGB24, codec_context ^ .width, codec_context ^ .height);
mybuffer: = av_malloc (numBytes * sizeof (cardinal));
Why the times sizeof(cardinal?), avpicture_get_size knows it is RGB24, so should already multiply with 3?
But then there is suddenly
avpicture_fill (PAVPicture (pFrameYUV420P), mybuffer, AV_PIX_FMT_RGB32, codec_context ^ .width, codec_context ^ .height);
where clearly suddenly is spoken about RGB32, IOW 4 bytes per pixel.
And then there swaptb that assumes everything is 4 bytes per pixel. Are you sure that pFrameYUV420P^.data[0] is the right pointer (and not e.g. @mybuffer[0]), and if it is really 32-bit per pixel ?
Hope I gave you a few pointers to look at.