Advertisement
Guest User

Untitled

a guest
Mar 27th, 2013
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.37 KB | None | 0 0
  1. static void video_encode_example(uint8_t *video_buffer)
  2. {
  3. AVCodec *codec;
  4. AVCodecContext *c= NULL;
  5. int i, out_size, size, x, y, outbuf_size;
  6. FILE *f;
  7. AVFrame *picture;
  8. uint8_t *outbuf, *picture_buf;
  9.  
  10. fprintf(stdout, "Video encoding\n");
  11.  
  12. /* register all the codecs */
  13. avcodec_register_all();
  14. avcodec_init();
  15. av_register_all();
  16.  
  17.  
  18. /* find the mpeg1 video encoder */
  19. codec = avcodec_find_encoder(CODEC_ID_MPEG2VIDEO);
  20. if (!codec) {
  21. fprintf(stderr, "codec not found\n");
  22. exit(1);
  23. }
  24.  
  25. c = avcodec_alloc_context();
  26. picture = avcodec_alloc_frame();
  27.  
  28. /* put sample parameters */
  29. c->bit_rate = 400000;
  30. /* resolution must be a multiple of two */
  31. c->width = 352;
  32. c->height = 288;
  33. /* frames per second */
  34. c->time_base= (AVRational){1,25};
  35. c->gop_size = 10; /* emit one intra frame every ten frames */
  36. c->max_b_frames=1;
  37. c->pix_fmt = PIX_FMT_YUV420P;
  38.  
  39. /* open it */
  40. if (avcodec_open(c, codec) < 0) {
  41. fprintf(stderr, "could not open codec\n");
  42. exit(1);
  43. }
  44.  
  45. /*
  46. f = fopen(filename, "wb");
  47. if (!f) {
  48. fprintf(stderr, "could not open %s\n", filename);
  49. exit(1);
  50. }
  51. */
  52.  
  53. /* alloc image and output buffer */
  54. int out_index = 0;
  55. outbuf_size = 100000;
  56. outbuf = static_cast<uint8_t *>(malloc(outbuf_size));
  57. size = c->width * c->height;
  58. picture_buf = static_cast<uint8_t *>(malloc((size * 3) / 2)); /* size for YUV 420 */
  59.  
  60. picture->data[0] = picture_buf;
  61. picture->data[1] = picture->data[0] + size;
  62. picture->data[2] = picture->data[1] + size / 4;
  63. picture->linesize[0] = c->width;
  64. picture->linesize[1] = c->width / 2;
  65. picture->linesize[2] = c->width / 2;
  66.  
  67. /* encode 1 second of video */
  68. for(i=0;i<25;i++) {
  69. //fflush(stdout);
  70. /* prepare a dummy image */
  71. /* Y */
  72. for(y=0;y<c->height;y++) {
  73. for(x=0;x<c->width;x++) {
  74. picture->data[0][y * picture->linesize[0] + x] = x + y + i * 3;
  75. }
  76. }
  77.  
  78. /* Cb and Cr */
  79. for(y=0;y<c->height/2;y++) {
  80. for(x=0;x<c->width/2;x++) {
  81. picture->data[1][y * picture->linesize[1] + x] = 128 + y + i * 2;
  82. picture->data[2][y * picture->linesize[2] + x] = 64 + x + i * 5;
  83. }
  84. }
  85. fprintf(stdout, "picture->data[0][0] = %d\n", picture->data[0][0]);
  86.  
  87. /* encode the image */
  88. out_size = avcodec_encode_video(c, outbuf, outbuf_size, picture);
  89. fprintf(stdout, "encoding frame %3d (size=%5d)\n", i, out_size);
  90. // Copy the image to the video buffer.
  91. memcpy(outbuf, video_buffer + out_index, out_size);
  92. out_index += out_size;
  93. int ri = 1000;
  94. fprintf(stdout, "outbuf[%d] = %d\n", ri, outbuf[ri]);
  95. //fwrite(outbuf, 1, out_size, f);
  96. }
  97.  
  98. /* get the delayed frames */
  99. for(; out_size; i++) {
  100. //fflush(stdout);
  101.  
  102. out_size = avcodec_encode_video(c, outbuf, outbuf_size, NULL);
  103. fprintf(stdout, "write frame %3d (size=%5d)\n", i, out_size);
  104. memcpy(outbuf, video_buffer + out_index, out_size);
  105. out_index += out_size;
  106. //fwrite(outbuf, 1, out_size, f);
  107. }
  108.  
  109. /* add sequence end code to have a real mpeg file */
  110. outbuf[0] = 0x00;
  111. outbuf[1] = 0x00;
  112. outbuf[2] = 0x01;
  113. outbuf[3] = 0xb7;
  114. memcpy(outbuf, video_buffer + out_index, 4);
  115. //fwrite(outbuf, 1, 4, f);
  116. //fclose(f);
  117. free(picture_buf);
  118. free(outbuf);
  119.  
  120. avcodec_close(c);
  121. av_free(c);
  122. av_free(picture);
  123. fprintf(stdout, "\n");
  124. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement