00001 /* 00002 * Copyright (c) 2010 The WebM project authors. All Rights Reserved. 00003 * 00004 * Use of this source code is governed by a BSD-style license 00005 * that can be found in the LICENSE file in the root of the source 00006 * tree. An additional intellectual property rights grant can be found 00007 * in the file PATENTS. All contributing project authors may 00008 * be found in the AUTHORS file in the root of the source tree. 00009 */ 00010 00011 // Simple Decoder 00012 // ============== 00013 // 00014 // This is an example of a simple decoder loop. It takes an input file 00015 // containing the compressed data (in IVF format), passes it through the 00016 // decoder, and writes the decompressed frames to disk. Other decoder 00017 // examples build upon this one. 00018 // 00019 // The details of the IVF format have been elided from this example for 00020 // simplicity of presentation, as IVF files will not generally be used by 00021 // your application. In general, an IVF file consists of a file header, 00022 // followed by a variable number of frames. Each frame consists of a frame 00023 // header followed by a variable length payload. The length of the payload 00024 // is specified in the first four bytes of the frame header. The payload is 00025 // the raw compressed data. 00026 // 00027 // Standard Includes 00028 // ----------------- 00029 // For decoders, you only have to include `vpx_decoder.h` and then any 00030 // header files for the specific codecs you use. In this case, we're using 00031 // vp8. 00032 // 00033 // Initializing The Codec 00034 // ---------------------- 00035 // The libvpx decoder is initialized by the call to vpx_codec_dec_init(). 00036 // Determining the codec interface to use is handled by VpxVideoReader and the 00037 // functions prefixed with vpx_video_reader_. Discussion of those functions is 00038 // beyond the scope of this example, but the main gist is to open the input file 00039 // and parse just enough of it to determine if it's a VPx file and which VPx 00040 // codec is contained within the file. 00041 // Note the NULL pointer passed to vpx_codec_dec_init(). We do that in this 00042 // example because we want the algorithm to determine the stream configuration 00043 // (width/height) and allocate memory automatically. 00044 // 00045 // Decoding A Frame 00046 // ---------------- 00047 // Once the frame has been read into memory, it is decoded using the 00048 // `vpx_codec_decode` function. The call takes a pointer to the data 00049 // (`frame`) and the length of the data (`frame_size`). No application data 00050 // is associated with the frame in this example, so the `user_priv` 00051 // parameter is NULL. The `deadline` parameter is left at zero for this 00052 // example. This parameter is generally only used when doing adaptive post 00053 // processing. 00054 // 00055 // Codecs may produce a variable number of output frames for every call to 00056 // `vpx_codec_decode`. These frames are retrieved by the 00057 // `vpx_codec_get_frame` iterator function. The iterator variable `iter` is 00058 // initialized to NULL each time `vpx_codec_decode` is called. 00059 // `vpx_codec_get_frame` is called in a loop, returning a pointer to a 00060 // decoded image or NULL to indicate the end of list. 00061 // 00062 // Processing The Decoded Data 00063 // --------------------------- 00064 // In this example, we simply write the encoded data to disk. It is 00065 // important to honor the image's `stride` values. 00066 // 00067 // Cleanup 00068 // ------- 00069 // The `vpx_codec_destroy` call frees any memory allocated by the codec. 00070 // 00071 // Error Handling 00072 // -------------- 00073 // This example does not special case any error return codes. If there was 00074 // an error, a descriptive message is printed and the program exits. With 00075 // few exceptions, vpx_codec functions return an enumerated error status, 00076 // with the value `0` indicating success. 00077 00078 #include <stdio.h> 00079 #include <stdlib.h> 00080 #include <string.h> 00081 00082 #include "vpx/vpx_decoder.h" 00083 00084 #include "../tools_common.h" 00085 #include "../video_reader.h" 00086 #include "./vpx_config.h" 00087 00088 static const char *exec_name; 00089 00090 void usage_exit(void) { 00091 fprintf(stderr, "Usage: %s <infile> <outfile>\n", exec_name); 00092 exit(EXIT_FAILURE); 00093 } 00094 00095 int main(int argc, char **argv) { 00096 int frame_cnt = 0; 00097 FILE *outfile = NULL; 00098 vpx_codec_ctx_t codec; 00099 VpxVideoReader *reader = NULL; 00100 const VpxInterface *decoder = NULL; 00101 const VpxVideoInfo *info = NULL; 00102 00103 exec_name = argv[0]; 00104 00105 if (argc != 3) die("Invalid number of arguments."); 00106 00107 reader = vpx_video_reader_open(argv[1]); 00108 if (!reader) die("Failed to open %s for reading.", argv[1]); 00109 00110 if (!(outfile = fopen(argv[2], "wb"))) 00111 die("Failed to open %s for writing.", argv[2]); 00112 00113 info = vpx_video_reader_get_info(reader); 00114 00115 decoder = get_vpx_decoder_by_fourcc(info->codec_fourcc); 00116 if (!decoder) die("Unknown input codec."); 00117 00118 printf("Using %s\n", vpx_codec_iface_name(decoder->codec_interface())); 00119 00120 if (vpx_codec_dec_init(&codec, decoder->codec_interface(), NULL, 0)) 00121 die_codec(&codec, "Failed to initialize decoder."); 00122 00123 while (vpx_video_reader_read_frame(reader)) { 00124 vpx_codec_iter_t iter = NULL; 00125 vpx_image_t *img = NULL; 00126 size_t frame_size = 0; 00127 const unsigned char *frame = 00128 vpx_video_reader_get_frame(reader, &frame_size); 00129 if (vpx_codec_decode(&codec, frame, (unsigned int)frame_size, NULL, 0)) 00130 die_codec(&codec, "Failed to decode frame."); 00131 00132 while ((img = vpx_codec_get_frame(&codec, &iter)) != NULL) { 00133 vpx_img_write(img, outfile); 00134 ++frame_cnt; 00135 } 00136 } 00137 00138 printf("Processed %d frames.\n", frame_cnt); 00139 if (vpx_codec_destroy(&codec)) die_codec(&codec, "Failed to destroy codec"); 00140 00141 printf("Play: ffplay -f rawvideo -pix_fmt yuv420p -s %dx%d %s\n", 00142 info->frame_width, info->frame_height, argv[2]); 00143 00144 vpx_video_reader_close(reader); 00145 00146 fclose(outfile); 00147 00148 return EXIT_SUCCESS; 00149 }