simple_decoder.c 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. /*
  2. * Copyright (c) 2010 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. // Simple Decoder
  11. // ==============
  12. //
  13. // This is an example of a simple decoder loop. It takes an input file
  14. // containing the compressed data (in IVF format), passes it through the
  15. // decoder, and writes the decompressed frames to disk. Other decoder
  16. // examples build upon this one.
  17. //
  18. // The details of the IVF format have been elided from this example for
  19. // simplicity of presentation, as IVF files will not generally be used by
  20. // your application. In general, an IVF file consists of a file header,
  21. // followed by a variable number of frames. Each frame consists of a frame
  22. // header followed by a variable length payload. The length of the payload
  23. // is specified in the first four bytes of the frame header. The payload is
  24. // the raw compressed data.
  25. //
  26. // Standard Includes
  27. // -----------------
  28. // For decoders, you only have to include `vpx_decoder.h` and then any
  29. // header files for the specific codecs you use. In this case, we're using
  30. // vp8.
  31. //
  32. // Initializing The Codec
  33. // ----------------------
  34. // The libvpx decoder is initialized by the call to vpx_codec_dec_init().
  35. // Determining the codec interface to use is handled by VpxVideoReader and the
  36. // functions prefixed with vpx_video_reader_. Discussion of those functions is
  37. // beyond the scope of this example, but the main gist is to open the input file
  38. // and parse just enough of it to determine if it's a VPx file and which VPx
  39. // codec is contained within the file.
  40. // Note the NULL pointer passed to vpx_codec_dec_init(). We do that in this
  41. // example because we want the algorithm to determine the stream configuration
  42. // (width/height) and allocate memory automatically.
  43. //
  44. // Decoding A Frame
  45. // ----------------
  46. // Once the frame has been read into memory, it is decoded using the
  47. // `vpx_codec_decode` function. The call takes a pointer to the data
  48. // (`frame`) and the length of the data (`frame_size`). No application data
  49. // is associated with the frame in this example, so the `user_priv`
  50. // parameter is NULL. The `deadline` parameter is left at zero for this
  51. // example. This parameter is generally only used when doing adaptive post
  52. // processing.
  53. //
  54. // Codecs may produce a variable number of output frames for every call to
  55. // `vpx_codec_decode`. These frames are retrieved by the
  56. // `vpx_codec_get_frame` iterator function. The iterator variable `iter` is
  57. // initialized to NULL each time `vpx_codec_decode` is called.
  58. // `vpx_codec_get_frame` is called in a loop, returning a pointer to a
  59. // decoded image or NULL to indicate the end of list.
  60. //
  61. // Processing The Decoded Data
  62. // ---------------------------
  63. // In this example, we simply write the encoded data to disk. It is
  64. // important to honor the image's `stride` values.
  65. //
  66. // Cleanup
  67. // -------
  68. // The `vpx_codec_destroy` call frees any memory allocated by the codec.
  69. //
  70. // Error Handling
  71. // --------------
  72. // This example does not special case any error return codes. If there was
  73. // an error, a descriptive message is printed and the program exits. With
  74. // few exceptions, vpx_codec functions return an enumerated error status,
  75. // with the value `0` indicating success.
  76. #include <stdio.h>
  77. #include <stdlib.h>
  78. #include <string.h>
  79. #include "vpx/vpx_decoder.h"
  80. #include "../tools_common.h"
  81. #include "../video_reader.h"
  82. #include "./vpx_config.h"
  83. static const char *exec_name;
  84. void usage_exit(void) {
  85. fprintf(stderr, "Usage: %s <infile> <outfile>\n", exec_name);
  86. exit(EXIT_FAILURE);
  87. }
  88. int main(int argc, char **argv) {
  89. int frame_cnt = 0;
  90. FILE *outfile = NULL;
  91. vpx_codec_ctx_t codec;
  92. VpxVideoReader *reader = NULL;
  93. const VpxInterface *decoder = NULL;
  94. const VpxVideoInfo *info = NULL;
  95. exec_name = argv[0];
  96. if (argc != 3) die("Invalid number of arguments.");
  97. reader = vpx_video_reader_open(argv[1]);
  98. if (!reader) die("Failed to open %s for reading.", argv[1]);
  99. if (!(outfile = fopen(argv[2], "wb")))
  100. die("Failed to open %s for writing.", argv[2]);
  101. info = vpx_video_reader_get_info(reader);
  102. decoder = get_vpx_decoder_by_fourcc(info->codec_fourcc);
  103. if (!decoder) die("Unknown input codec.");
  104. printf("Using %s\n", vpx_codec_iface_name(decoder->codec_interface()));
  105. if (vpx_codec_dec_init(&codec, decoder->codec_interface(), NULL, 0))
  106. die_codec(&codec, "Failed to initialize decoder.");
  107. while (vpx_video_reader_read_frame(reader)) {
  108. vpx_codec_iter_t iter = NULL;
  109. vpx_image_t *img = NULL;
  110. size_t frame_size = 0;
  111. const unsigned char *frame =
  112. vpx_video_reader_get_frame(reader, &frame_size);
  113. if (vpx_codec_decode(&codec, frame, (unsigned int)frame_size, NULL, 0))
  114. die_codec(&codec, "Failed to decode frame.");
  115. while ((img = vpx_codec_get_frame(&codec, &iter)) != NULL) {
  116. vpx_img_write(img, outfile);
  117. ++frame_cnt;
  118. }
  119. }
  120. printf("Processed %d frames.\n", frame_cnt);
  121. if (vpx_codec_destroy(&codec)) die_codec(&codec, "Failed to destroy codec");
  122. printf("Play: ffplay -f rawvideo -pix_fmt yuv420p -s %dx%d %s\n",
  123. info->frame_width, info->frame_height, argv[2]);
  124. vpx_video_reader_close(reader);
  125. fclose(outfile);
  126. return EXIT_SUCCESS;
  127. }