video_writer.c 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. /*
  2. * Copyright (c) 2014 The WebM project authors. All Rights Reserved.
  3. *
  4. * Use of this source code is governed by a BSD-style license
  5. * that can be found in the LICENSE file in the root of the source
  6. * tree. An additional intellectual property rights grant can be found
  7. * in the file PATENTS. All contributing project authors may
  8. * be found in the AUTHORS file in the root of the source tree.
  9. */
  10. #include <stdlib.h>
  11. #include "./ivfenc.h"
  12. #include "./video_writer.h"
  13. #include "vpx/vpx_encoder.h"
  14. struct VpxVideoWriterStruct {
  15. VpxVideoInfo info;
  16. FILE *file;
  17. int frame_count;
  18. };
  19. static void write_header(FILE *file, const VpxVideoInfo *info,
  20. int frame_count) {
  21. struct vpx_codec_enc_cfg cfg;
  22. cfg.g_w = info->frame_width;
  23. cfg.g_h = info->frame_height;
  24. cfg.g_timebase.num = info->time_base.numerator;
  25. cfg.g_timebase.den = info->time_base.denominator;
  26. ivf_write_file_header(file, &cfg, info->codec_fourcc, frame_count);
  27. }
  28. VpxVideoWriter *vpx_video_writer_open(const char *filename,
  29. VpxContainer container,
  30. const VpxVideoInfo *info) {
  31. if (container == kContainerIVF) {
  32. VpxVideoWriter *writer = NULL;
  33. FILE *const file = fopen(filename, "wb");
  34. if (!file) return NULL;
  35. writer = malloc(sizeof(*writer));
  36. if (!writer) return NULL;
  37. writer->frame_count = 0;
  38. writer->info = *info;
  39. writer->file = file;
  40. write_header(writer->file, info, 0);
  41. return writer;
  42. }
  43. return NULL;
  44. }
  45. void vpx_video_writer_close(VpxVideoWriter *writer) {
  46. if (writer) {
  47. // Rewriting frame header with real frame count
  48. rewind(writer->file);
  49. write_header(writer->file, &writer->info, writer->frame_count);
  50. fclose(writer->file);
  51. free(writer);
  52. }
  53. }
  54. int vpx_video_writer_write_frame(VpxVideoWriter *writer, const uint8_t *buffer,
  55. size_t size, int64_t pts) {
  56. ivf_write_frame_header(writer->file, pts, size);
  57. if (fwrite(buffer, 1, size, writer->file) != size) return 0;
  58. ++writer->frame_count;
  59. return 1;
  60. }