Guest User

Untitled

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