Advertisement
pleasedontcode

H.264 Encoding on ESP32

Oct 15th, 2024
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Arduino 1.59 KB | Source Code | 0 0
  1. #include "h264_encoder.h"
  2. #include "esp_camera.h"
  3.  
  4. // Camera config
  5. camera_config_t config;
  6. config.ledc_channel = LEDC_CHANNEL_0;
  7. config.ledc_timer = LEDC_TIMER_0;
  8. config.pin_d0 = Y2_GPIO_NUM;
  9. config.pin_d1 = Y3_GPIO_NUM;
  10. config.pin_d2 = Y4_GPIO_NUM;
  11. config.pin_d3 = Y5_GPIO_NUM;
  12. config.pin_d4 = Y6_GPIO_NUM;
  13. config.pin_d5 = Y7_GPIO_NUM;
  14. config.pin_d6 = Y8_GPIO_NUM;
  15. config.pin_d7 = Y9_GPIO_NUM;
  16. config.pin_xclk = XCLK_GPIO_NUM;
  17. config.pin_pclk = PCLK_GPIO_NUM;
  18. config.pin_vsync = VSYNC_GPIO_NUM;
  19. config.pin_href = HREF_GPIO_NUM;
  20. config.pin_sscb_sda = SIOD_GPIO_NUM;
  21. config.pin_sscb_scl = SIOC_GPIO_NUM;
  22. config.pin_pwdn = PWDN_GPIO_NUM;
  23. config.pin_reset = RESET_GPIO_NUM;
  24. config.xclk_freq_hz = 20000000;
  25. config.pixel_format = PIXFORMAT_JPEG;
  26. config.frame_size = FRAMESIZE_QVGA; //320x240
  27. config.jpeg_quality = 12;
  28. config.fb_count = 1;
  29.  
  30. void setup() {
  31.   Serial.begin(115200);
  32.  
  33.   // Initialize camera
  34.   if (esp_camera_init(&config) != ESP_OK) {
  35.     Serial.println("Camera init failed");
  36.     return;
  37.   }
  38.  
  39.   // Initialize H.264 encoder
  40.   h264_encoder_init(320, 240);
  41.  
  42.   Serial.println("Camera and H.264 encoder initialized");
  43. }
  44.  
  45. void loop() {
  46.   // Capture frame
  47.   camera_fb_t *fb = esp_camera_fb_get();
  48.  
  49.   if (!fb) {
  50.     Serial.println("Camera capture failed");
  51.     return;
  52.   }
  53.  
  54.   // Encode to H.264
  55.   uint8_t *encoded_data;
  56.   size_t encoded_size;
  57.   if (h264_encoder_encode_frame(fb->buf, fb->len, &encoded_data, &encoded_size)) {
  58.     Serial.println("Frame encoded successfully");
  59.     // Handle encoded data, e.g., save or stream
  60.   }
  61.  
  62.   esp_camera_fb_return(fb);
  63. }
  64.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement