On Sat, Jun 30, 2012 at 4:33 AM, Tank2005 <
sint...@gmail.com> wrote:
> I am developing the program which makes webm video using libvpx. Although I
> would like to change Windows Bitmap into a frame picture, since the buffer
> management of libvpx is individual, causes a memory leak and incorrect
> conversion (an upper half becomes purple). Please let me know the right
> coding.
>
A simpler option might be to use libyuv:
http://code.google.com/p/libyuv/source/browse/trunk/include/libyuv/convert.h
See this link [1] for an example of the inverse operation.
[1]
https://groups.google.com/a/webmproject.org/d/msg/apps-devel/z0MCTHRXo-I/byoF553spCsJ
> This is Visual Studio 2010 project in which all the source codes were
> contained. Since you can test a program, please try it.
>
https://skydrive.live.com/redir?resid=74EABB0E82700B4D!402
>
> -------------------------------------
> #define rgbtoy(b, g, r, y) \
> y=(unsigned char)(((int)(30*r) + (int)(59*g) + (int)(11*b))/100)
> #define rgbtoyuv(b, g, r, y, u, v) \
> rgbtoy(b, g, r, y); \
> u=(unsigned char)(((int)(-17*r) - (int)(33*g) + (int)(50*b)+12800)/100); \
> v=(unsigned char)(((int)(50*r) - (int)(42*g) - (int)(8*b)+12800)/100)
>
> int EncodeFrame(void* vdata, unsigned char *buffer, unsigned char
> **res_buffer, int *res_length)
> {
> VpxEncData *data = (VpxEncData*)vdata;
>
> int width = data->image.w, height = data->image.h;
>
> unsigned char *yplane = data->image.planes[0];
> unsigned char *uplane = data->image.planes[1];
> unsigned char *vplane = data->image.planes[2];
>
> for (int y = 0; y < height; y++){
> unsigned char *yline = yplane;
> unsigned char *uline = uplane;
> unsigned char *vline = vplane;
> for (int x = 0; x < width; x+=2){
> int pos = data->rowbytes * y + x * 3;
> rgbtoyuv(buffer[pos + 2], buffer[pos + 1], buffer[pos], *yline,
> *uline, *vline);
> pos += 3;
> yline++;
> rgbtoyuv(buffer[pos + 2], buffer[pos + 1], buffer[pos], *yline,
> *uline, *vline);
> yline++;
> uline++;
> vline++;
> }
> yplane += data->image.stride[0];
> uplane += data->image.stride[1];
> vplane += data->image.stride[2];
> }
>
> vpx_codec_err_t err = vpx_codec_encode(&data->codec, &data->image,
> data->frames, 1, 0, VPX_DL_REALTIME);
> if(err) return err;
>
> vpx_codec_iter_t iter = NULL;
> const vpx_codec_cx_pkt_t *pkt = vpx_codec_get_cx_data(&data->codec,
> &iter);
> if(pkt->kind == VPX_CODEC_CX_FRAME_PKT){
> *res_buffer = (unsigned char*)pkt->data.frame.buf;
> *res_length = pkt->
data.frame.sz;
> }
> data->frames++;
> return 0;
> }
>
> inline unsigned char NUMTRIM(int n)
> {
> if(n < 0) return 0; else if (n > 255) return 255; else return (unsigned
> char)n;
> }
>
> int DecodeFrame(void *data, unsigned char *buffer, int buflen, unsigned char
> *resbuf)
> {
> VpxDecData *dat = (VpxDecData*)data;
> vpx_codec_err_t err = vpx_codec_decode(&dat->codec, buffer, buflen,
> NULL, 0);
> if(err) return err;
>
> vpx_codec_iter_t iter = NULL;
> vpx_image_t *img = vpx_codec_get_frame(&dat->codec, &iter);
> if(img){
> int height = img->d_h, width = img->d_w;
> unsigned char *ybuf = img->planes[0];
> unsigned char *ubuf = img->planes[1];
> unsigned char *vbuf = img->planes[2];
> for(int y = 0; y < height; y++){
> unsigned char *yy = ybuf, *uu = ubuf, *vv = vbuf;
> for(int x = 0; x < width; x++){
> int y0 = ((*yy) - 16) * 100000, u0 = (int)(*uu) - 128, v0 =
> (int)(*vv) - 128;
> int r = (y0 + 140200 * v0) / 100000;
> int g = (y0 - 34414 * u0 - 71414 * v0) / 100000;
> int b = (y0 + 177200 * u0) / 100000;
>
> int pos = y * dat->rowbytes + x * 3;
> resbuf[pos] = NUMTRIM(b);
> resbuf[pos + 1] = NUMTRIM(g);
> resbuf[pos + 2] = NUMTRIM(r);
>
> yy++;
> uu++;
> vv++;
> }
> ybuf += img->stride[0];
> ubuf += img->stride[1];
> vbuf += img->stride[2];
> }
> }else{
> memset(resbuf, 0, dat->rowbytes * img->d_h);
> }
>
>
> vpx_codec_iter_t iter = NULL;
> vpx_image_t *img = vpx_codec_get_frame(&dat->codec, &iter);
> if(img){
> int height = img->d_h, width = img->d_w;
> }else{
> memset(resbuf, 0, dat->rowbytes * img->d_h);
> }
>
> return 0;
> }
>
> --
> You received this message because you are subscribed to the Google Groups
> "WebM Discussion" group.
> To view this discussion on the web visit
>
https://groups.google.com/a/webmproject.org/d/msg/webm-discuss/-/gg0qksJTtE4J.
> To post to this group, send email to
webm-d...@webmproject.org.
> To unsubscribe from this group, send email to
>
webm-discuss...@webmproject.org.
> For more options, visit this group at
>
http://groups.google.com/a/webmproject.org/group/webm-discuss/?hl=en.