1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250
|
/*
* Copyright 2018, James Cowgill <jcowgill@debian.org>
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in
* the documentation and/or other materials provided with the
* distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
* COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
* BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
* ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*/
#include <aom/aomcx.h>
#include <aom/aomdx.h>
#include <aom/aom_encoder.h>
#include <aom/aom_decoder.h>
#include <assert.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
/*
* Simple smoke test program to work the encoder and decoder
*
* - Generate simple test frame (200x200 all black)
* - Encode it
* - Decode it
* - Test that the result is all black (while not strictly correct, hopefully
* the encoder can do this!)
*/
#define FRAME_WIDTH 200
#define FRAME_HEIGHT 200
// Exit on error
void die_on_fail(aom_codec_err_t result, aom_codec_ctx_t* ctx, const char* func)
{
if (result != AOM_CODEC_OK) {
fprintf(stderr, "error at %s (%s)\n", func, aom_codec_err_to_string(result));
if (ctx != NULL)
fputs(aom_codec_error_detail(ctx), stderr);
abort();
}
}
// Header for packet linked list entries
struct packet_ll_head {
struct packet_ll_head* next;
size_t size;
unsigned char buf[];
};
// Linked list for storing packet data
struct packet_ll {
struct packet_ll_head* first;
struct packet_ll_head* last;
};
// Appends a compressed video frame packet to the list (other packets ignored)
void packet_ll_append(struct packet_ll* list, const aom_codec_cx_pkt_t* packet)
{
if (packet->kind != AOM_CODEC_CX_FRAME_PKT)
return;
// Allocate new list head and copy data
struct packet_ll_head* new_head = malloc(sizeof(struct packet_ll_head) + packet->data.frame.sz);
if (new_head == NULL)
die_on_fail(AOM_CODEC_MEM_ERROR, NULL, "malloc");
new_head->next = NULL;
new_head->size = packet->data.frame.sz;
memcpy(new_head->buf, packet->data.frame.buf, packet->data.frame.sz);
// Append head to the list
if (list->last == NULL)
list->first = new_head;
else
list->last->next = new_head;
list->last = new_head;
}
// Returns the size of a packet list
int packet_ll_size(const struct packet_ll* list)
{
const struct packet_ll_head* current = list->first;
int size = 0;
while (current) {
size++;
current = current->next;
}
return size;
}
// Frees a packet list and all its elements
void packet_ll_free(struct packet_ll* list)
{
struct packet_ll_head* current = list->first;
while (current) {
struct packet_ll_head* next = current->next;
free(current);
current = next;
}
list->first = NULL;
list->last = NULL;
}
// Encodes a frame and writes any packets to the list
// Returns nuber of appended packets
int encode_frame(aom_codec_ctx_t* ctx, const aom_image_t* img, int index, int flags, struct packet_ll* list)
{
die_on_fail(aom_codec_encode(ctx, img, index, 1, flags), ctx, "aom_codec_encode");
const aom_codec_cx_pkt_t* packet;
aom_codec_iter_t iter = NULL;
int packets = 0;
while ((packet = aom_codec_get_cx_data(ctx, &iter)) != NULL) {
packet_ll_append(list, packet);
packets++;
}
return packets;
}
// Encodes a blank image and returns the packet data
struct packet_ll encode_black_image(void)
{
// Create blank image
aom_image_t* img = aom_img_alloc(NULL, AOM_IMG_FMT_I420 , FRAME_WIDTH, FRAME_HEIGHT, 1);
if (img == NULL)
die_on_fail(AOM_CODEC_MEM_ERROR, NULL, "aom_img_alloc");
for (int plane = 0; plane < 3; plane++) {
unsigned char* buf = img->planes[plane];
const int stride = img->stride[plane];
const int w = aom_img_plane_width(img, plane);
const int h = aom_img_plane_height(img, plane);
for (int line = 0; line < h; line++, buf += stride)
memset(buf, 0, w);
}
// Initialize encoder
aom_codec_enc_cfg_t enc_config;
die_on_fail(aom_codec_enc_config_default(aom_codec_av1_cx(), &enc_config, 0),
NULL, "aom_codec_enc_config_default");
enc_config.g_w = FRAME_WIDTH;
enc_config.g_h = FRAME_HEIGHT;
enc_config.g_timebase.num = 1;
enc_config.g_timebase.den = 30;
aom_codec_ctx_t ctx;
die_on_fail(aom_codec_enc_init(&ctx, aom_codec_av1_cx(), &enc_config, 0),
NULL, "aom_codec_enc_init");
// Encode frame
struct packet_ll encoded = { NULL, NULL };
encode_frame(&ctx, img, 0, AOM_EFLAG_FORCE_KF, &encoded);
// Flush encoder, destroy context and destroy image
while (encode_frame(&ctx, NULL, -1, 0, &encoded));
aom_codec_destroy(&ctx);
aom_img_free(img);
return encoded;
}
// Decodes the first frame of a packet list and returns it
aom_image_t* decode_first_frame(aom_codec_ctx_t* ctx, const struct packet_ll* encoded)
{
// Initialize Decoder
die_on_fail(aom_codec_dec_init(ctx, aom_codec_av1_dx(), NULL, 0),
NULL, "aom_codec_dec_init");
// Decode packets until we get the first frame
struct packet_ll_head* current = encoded->first;
while (current) {
die_on_fail(aom_codec_decode(ctx, current->buf, current->size, NULL),
ctx, "aom_codec_decode");
aom_codec_iter_t iter = NULL;
aom_image_t* img = aom_codec_get_frame(ctx, &iter);
if (img != NULL)
return img;
current = current->next;
}
die_on_fail(AOM_CODEC_ERROR, NULL, "no packets decoded");
return NULL;
}
int main(void)
{
printf("aom library version: %s\n", aom_codec_version_str());
printf("aom library config: %s\n", aom_codec_build_config());
// Run one encode / decode cycle
puts("encoding...");
struct packet_ll encoded = encode_black_image();
int encoded_size = packet_ll_size(&encoded);
printf("encoded into %i packets\n", encoded_size);
assert(encoded_size > 0);
puts("decoding...");
aom_codec_ctx_t decode_ctx;
aom_image_t* decoded = decode_first_frame(&decode_ctx, &encoded);
// Verify the decoded image is black and the right size
assert(decoded->d_w == FRAME_WIDTH);
assert(decoded->d_h == FRAME_HEIGHT);
for (int plane = 0; plane < 3; plane++) {
unsigned char* buf = decoded->planes[plane];
const int stride = decoded->stride[plane];
const int w = aom_img_plane_width(decoded, plane);
const int h = aom_img_plane_height(decoded, plane);
for (int line = 0; line < h; line++, buf += stride)
for (int x = 0; x < w; x++)
assert(buf[x] == 0);
}
puts("done!");
packet_ll_free(&encoded);
aom_codec_destroy(&decode_ctx);
return 0;
}
|