edith_codecs
Clone
https://git.tahinli.com/Tahinli/edith_codecs.git
0 watching·0 forks·Rust·370.3 MiB·9b67700·Tahinli·3 days ago·4021 commits
| 1 | #include <stdio.h> |
| 2 | #include <stdlib.h> |
| 3 | #include <string.h> |
| 4 | #include <stdint.h> |
| 5 | #include <vpx/vpx_encoder.h> |
| 6 | #include <vpx/vp8cx.h> |
| 7 | static FILE *O; |
| 8 | static void drain(vpx_codec_ctx_t *c){ |
| 9 | const vpx_codec_cx_pkt_t *pkt; vpx_codec_iter_t it=NULL; |
| 10 | while((pkt=vpx_codec_get_cx_data(c,&it))) if(pkt->kind==VPX_CODEC_CX_FRAME_PKT){ |
| 11 | unsigned char sz[4]; uint32_t n=pkt->data.frame.sz; memcpy(sz,&n,4); fwrite(sz,1,4,O); |
| 12 | unsigned long ts=(unsigned long)pkt->data.frame.pts; unsigned char t[8]; memcpy(t,&ts,8); fwrite(t,1,8,O); |
| 13 | fwrite(pkt->data.frame.buf,1,n,O); |
| 14 | } |
| 15 | } |
| 16 | static void fill(vpx_image_t*img,int f){ |
| 17 | int W=img->d_w,H=img->d_h; |
| 18 | for(int y=0;y<H;y++)for(int x=0;x<W;x++){ |
| 19 | int v=(x*160/W)+(y*120/H)+f*4; |
| 20 | img->planes[0][y*img->stride[0]+x]=(unsigned char)(v&0xff);} |
| 21 | // A moving 200x120 high-contrast box (nonzero MVs + residual under scaling). |
| 22 | int bx=(f*37)%(W-200), by=(f*23)%(H-120); |
| 23 | for(int y=by;y<by+120;y++)for(int x=bx;x<bx+200;x++) |
| 24 | img->planes[0][y*img->stride[0]+x]=(((x/16+y/16)&1)?230:25); |
| 25 | for(int p=1;p<3;p++){int cw=(W+1)/2,ch=(H+1)/2; |
| 26 | for(int y=0;y<ch;y++)for(int x=0;x<cw;x++){ |
| 27 | int v=128+(x*40/cw)-(y*30/ch)+f*(p?2:-2); |
| 28 | img->planes[p][y*img->stride[p]+x]=(unsigned char)(v&0xff);}} |
| 29 | } |
| 30 | int main(int argc,char**argv){ |
| 31 | const char*out=argv[1]; int W=atoi(argv[2]),H=atoi(argv[3]),N=atoi(argv[4]),br=atoi(argv[5]); |
| 32 | vpx_codec_enc_cfg_t cfg; vpx_codec_enc_config_default(vpx_codec_vp9_cx(),&cfg,0); |
| 33 | cfg.g_w=W;cfg.g_h=H;cfg.g_timebase.num=1;cfg.g_timebase.den=30; |
| 34 | cfg.rc_end_usage=VPX_CBR;cfg.rc_target_bitrate=br;cfg.g_lag_in_frames=0; |
| 35 | cfg.rc_resize_allowed=1; cfg.rc_buf_sz=200; cfg.rc_buf_initial_sz=100; cfg.rc_buf_optimal_sz=150; |
| 36 | cfg.rc_min_quantizer=4; cfg.rc_max_quantizer=56; |
| 37 | cfg.kf_min_dist=9999;cfg.kf_max_dist=9999; |
| 38 | vpx_codec_ctx_t c; |
| 39 | if(vpx_codec_enc_init(&c,vpx_codec_vp9_cx(),&cfg,0)){fprintf(stderr,"init %s\n",vpx_codec_error(&c));return 1;} |
| 40 | vpx_codec_control(&c,VP8E_SET_CPUUSED,5); |
| 41 | O=fopen(out,"wb"); unsigned char hdr[32];memset(hdr,0,32);memcpy(hdr,"DKIF",4);hdr[6]=32;memcpy(hdr+8,"VP90",4); |
| 42 | hdr[12]=W&0xff;hdr[13]=W>>8;hdr[14]=H&0xff;hdr[15]=H>>8;hdr[16]=30;hdr[17]=1;hdr[20]=1; |
| 43 | uint32_t nf=N;memcpy(hdr+24,&nf,4);fwrite(hdr,1,32,O); |
| 44 | vpx_image_t*img=vpx_img_alloc(NULL,VPX_IMG_FMT_I420,W,H,1); |
| 45 | for(int i=0;i<N;i++){ fill(img,i); if(vpx_codec_encode(&c,img,i,1,0,VPX_DL_GOOD_QUALITY)){fprintf(stderr,"enc %d %s\n",i,vpx_codec_error(&c));return 1;} drain(&c); } |
| 46 | for(;;){vpx_codec_encode(&c,NULL,N,1,0,VPX_DL_GOOD_QUALITY);int b=ftell(O);drain(&c);if(ftell(O)==b)break;} |
| 47 | fclose(O);vpx_codec_destroy(&c);fprintf(stderr,"wrote %s %dx%d n=%d br=%d\n",out,W,H,N,br);return 0; |
| 48 | } |