ios - Getting YUV from CMSampleBufferRef for video streaming -
i building ios video streaming chat application , library working requires send video data passing yuv (or guess ycbcr) data individually.
i have delegate set up, i'm not sure how individual yuv elements cmsamplebufferref
. alot of apple guides seen, reference stuff capturing video frames uiimages though.
stream format
- (bool)setupwitherror:(nserror **)error { avcapturedevice *videocapturedevice = [avcapturedevice defaultdevicewithmediatype:avmediatypevideo]; avcapturedeviceinput *videoinput = [avcapturedeviceinput deviceinputwithdevice:videocapturedevice error:error]; if (! videoinput) { return no; } [self.capturesession addinput:videoinput]; self.processingqueue = dispatch_queue_create("abcdefghijk", null); [self.dataoutput setalwaysdiscardslatevideoframes:yes]; nsnumber *value = [nsnumber numberwithunsignedint:kcvpixelformattype_420ypcbcr8biplanarvideorange]; [self.dataoutput setvideosettings:[nsdictionary dictionarywithobject:value forkey:(id)kcvpixelbufferpixelformattypekey]]; [self.dataoutput setsamplebufferdelegate:self queue:self.processingqueue]; return yes; } - (void)captureoutput:(avcaptureoutput *)captureoutput didoutputsamplebuffer:(cmsamplebufferref)samplebuffer fromconnection:(avcaptureconnection *)connection { cvimagebufferref imagebuffer = cmsamplebuffergetimagebuffer(samplebuffer); if (! imagebuffer) { return; } uint16_t width = cvpixelbuffergetheight(imagebuffer); uint16_t height = cvpixelbuffergetheight(imagebuffer); uint8_t yplane[??] = ??? uint8_t uplane[?] = ??? uint8_t vplane[?] = ??? [self.library sendvideoframetofriend:self.friendnumber width:width height:height yplane:yplane uplane:uplane vplane:vplane error:nil]; }
does have examples or links can figure out?
update according https://wiki.videolan.org/yuv there shall more elements of y there u/v. library confirms well, nothing below:
* y - plane should of size: height * width * u - plane should of size: (height/2) * (width/2) * v - plane should of size: (height/2) * (width/2)
updated i've read how yuv buffer composed , how read it. made sure dont malloc on every frame.
have fun! ;)
//int bufferwidth = cvpixelbuffergetwidth(pixelbuffer); int yheight = cvpixelbuffergetheightofplane(pixelbuffer,0); int uvheight = cvpixelbuffergetheightofplane(pixelbuffer,1); int ywidth = cvpixelbuffergetwidthofplane(pixelbuffer,0); int uvwidth = cvpixelbuffergetwidthofplane(pixelbuffer,1); int ybpr = cvpixelbuffergetbytesperrowofplane(pixelbuffer, 0); int uvbpr = cvpixelbuffergetbytesperrowofplane(pixelbuffer, 1); int ysize = yheight * ybpr ; int uvsize = uvheight * uvbpr ; static unsigned char *ypane; if(!ypane) ypane = (unsigned char*)malloc(ysize); static unsigned char *upane; if(!upane) upane = (unsigned char*)malloc(uvsize); static unsigned char *vpane; if(!vpane) vpane = (unsigned char*)malloc(uvsize); unsigned char *ybase = cvpixelbuffergetbaseaddressofplane(ypane, 0); unsigned char *ubase = cvpixelbuffergetbaseaddressofplane(upane, 1; unsigned char *vbase = cvpixelbuffergetbaseaddressofplane(vpane, 2); for(int y=0,y<yheight;y++) { for(int x=0,x<ywidth;x++) { ypane[y*ywidth+x]=ybase[y*ybpr+x]; } } for(int y=0,y<uvheight;y++) { for(int x=0,x<uvwidth;x++) { upane[y*uvwidth+x]=ubase[y*uvbpr+x]; vpane[y*uvwidth+x]=vbase[y*uvbpr+x]; } }
Comments
Post a Comment