]> granicus.if.org Git - libvpx/blob - vpx/src/svc_encodeframe.c
Merge "Changes SvcContext_t to SvcContext"
[libvpx] / vpx / src / svc_encodeframe.c
1 /*
2  *  Copyright (c) 2013 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
11 /**
12  * @file
13  * VP9 SVC encoding support via libvpx
14  */
15
16 #include <assert.h>
17 #include <math.h>
18 #include <limits.h>
19 #include <stdarg.h>
20 #include <stdio.h>
21 #include <stdlib.h>
22 #include <string.h>
23 #define VPX_DISABLE_CTRL_TYPECHECKS 1
24 #include "./vpx_config.h"
25 #include "vpx/svc_context.h"
26 #include "vpx/vp8cx.h"
27 #include "vpx/vpx_encoder.h"
28 #include "vpx_mem/vpx_mem.h"
29 #include "vp9/common/vp9_onyxc_int.h"
30
31 #ifdef __MINGW32__
32 #define strtok_r strtok_s
33 #ifndef MINGW_HAS_SECURE_API
34 // proto from /usr/x86_64-w64-mingw32/include/sec_api/string_s.h
35 _CRTIMP char *__cdecl strtok_s(char *str, const char *delim, char **context);
36 #endif  /* MINGW_HAS_SECURE_API */
37 #endif  /* __MINGW32__ */
38
39 #ifdef _MSC_VER
40 #define strdup _strdup
41 #define strtok_r strtok_s
42 #endif
43
44 #define SVC_REFERENCE_FRAMES 8
45 #define SUPERFRAME_SLOTS (8)
46 #define SUPERFRAME_BUFFER_SIZE (SUPERFRAME_SLOTS * sizeof(uint32_t) + 2)
47
48 #define MAX_QUANTIZER 63
49
50 static const int DEFAULT_SCALE_FACTORS_NUM[VPX_SS_MAX_LAYERS] = {
51   4, 5, 7, 11, 16
52 };
53
54 static const int DEFAULT_SCALE_FACTORS_DEN[VPX_SS_MAX_LAYERS] = {
55   16, 16, 16, 16, 16
56 };
57
58 typedef enum {
59   QUANTIZER = 0,
60   BITRATE,
61   SCALE_FACTOR,
62   AUTO_ALT_REF,
63   ALL_OPTION_TYPES
64 } LAYER_OPTION_TYPE;
65
66 static const int option_max_values[ALL_OPTION_TYPES] = {
67   63, INT_MAX, INT_MAX, 1
68 };
69
70 static const int option_min_values[ALL_OPTION_TYPES] = {
71   0, 0, 1, 0
72 };
73
74 // One encoded frame
75 typedef struct FrameData {
76   void                     *buf;    // compressed data buffer
77   size_t                    size;  // length of compressed data
78   vpx_codec_frame_flags_t   flags;    /**< flags for this frame */
79   struct FrameData         *next;
80 } FrameData;
81
82 static SvcInternal_t *get_svc_internal(SvcContext *svc_ctx) {
83   if (svc_ctx == NULL) return NULL;
84   if (svc_ctx->internal == NULL) {
85     SvcInternal_t *const si = (SvcInternal_t *)malloc(sizeof(*si));
86     if (si != NULL) {
87       memset(si, 0, sizeof(*si));
88     }
89     svc_ctx->internal = si;
90   }
91   return (SvcInternal_t *)svc_ctx->internal;
92 }
93
94 static const SvcInternal_t *get_const_svc_internal(
95     const SvcContext *svc_ctx) {
96   if (svc_ctx == NULL) return NULL;
97   return (const SvcInternal_t *)svc_ctx->internal;
98 }
99
100 static void svc_log_reset(SvcContext *svc_ctx) {
101   SvcInternal_t *const si = (SvcInternal_t *)svc_ctx->internal;
102   si->message_buffer[0] = '\0';
103 }
104
105 static int svc_log(SvcContext *svc_ctx, SVC_LOG_LEVEL level,
106                    const char *fmt, ...) {
107   char buf[512];
108   int retval = 0;
109   va_list ap;
110   SvcInternal_t *const si = get_svc_internal(svc_ctx);
111
112   if (level > svc_ctx->log_level) {
113     return retval;
114   }
115
116   va_start(ap, fmt);
117   retval = vsnprintf(buf, sizeof(buf), fmt, ap);
118   va_end(ap);
119
120   if (svc_ctx->log_print) {
121     printf("%s", buf);
122   } else {
123     strncat(si->message_buffer, buf,
124             sizeof(si->message_buffer) - strlen(si->message_buffer) - 1);
125   }
126
127   if (level == SVC_LOG_ERROR) {
128     si->codec_ctx->err_detail = si->message_buffer;
129   }
130   return retval;
131 }
132
133 static vpx_codec_err_t extract_option(LAYER_OPTION_TYPE type,
134                                       char *input,
135                                       int *value0,
136                                       int *value1) {
137   if (type == SCALE_FACTOR) {
138     *value0 = strtol(input, &input, 10);
139     if (*input++ != '/')
140       return VPX_CODEC_INVALID_PARAM;
141     *value1 = strtol(input, &input, 10);
142
143     if (*value0 < option_min_values[SCALE_FACTOR] ||
144         *value1 < option_min_values[SCALE_FACTOR] ||
145         *value0 > option_max_values[SCALE_FACTOR] ||
146         *value1 > option_max_values[SCALE_FACTOR] ||
147         *value0 > *value1)  // num shouldn't be greater than den
148       return VPX_CODEC_INVALID_PARAM;
149   } else {
150     *value0 = atoi(input);
151     if (*value0 < option_min_values[type] ||
152         *value0 > option_max_values[type])
153       return VPX_CODEC_INVALID_PARAM;
154   }
155   return VPX_CODEC_OK;
156 }
157
158 static vpx_codec_err_t parse_layer_options_from_string(SvcContext *svc_ctx,
159                                                        LAYER_OPTION_TYPE type,
160                                                        const char *input,
161                                                        int *option0,
162                                                        int *option1) {
163   int i;
164   vpx_codec_err_t res = VPX_CODEC_OK;
165   char *input_string;
166   char *token;
167   const char *delim = ",";
168   char *save_ptr;
169
170   if (input == NULL || option0 == NULL ||
171       (option1 == NULL && type == SCALE_FACTOR))
172     return VPX_CODEC_INVALID_PARAM;
173
174   input_string = strdup(input);
175   token = strtok_r(input_string, delim, &save_ptr);
176   for (i = 0; i < svc_ctx->spatial_layers; ++i) {
177     if (token != NULL) {
178       res = extract_option(type, token, option0 + i, option1 + i);
179       if (res != VPX_CODEC_OK)
180         break;
181       token = strtok_r(NULL, delim, &save_ptr);
182     } else {
183       break;
184     }
185   }
186   if (res == VPX_CODEC_OK && i != svc_ctx->spatial_layers) {
187     svc_log(svc_ctx, SVC_LOG_ERROR,
188             "svc: layer params type: %d    %d values required, "
189             "but only %d specified\n", type, svc_ctx->spatial_layers, i);
190     res = VPX_CODEC_INVALID_PARAM;
191   }
192   free(input_string);
193   return res;
194 }
195
196 /**
197  * Parse SVC encoding options
198  * Format: encoding-mode=<svc_mode>,layers=<layer_count>
199  *         scale-factors=<n1>/<d1>,<n2>/<d2>,...
200  *         quantizers=<q1>,<q2>,...
201  * svc_mode = [i|ip|alt_ip|gf]
202  */
203 static vpx_codec_err_t parse_options(SvcContext *svc_ctx, const char *options) {
204   char *input_string;
205   char *option_name;
206   char *option_value;
207   char *input_ptr;
208   SvcInternal_t *const si = get_svc_internal(svc_ctx);
209   vpx_codec_err_t res = VPX_CODEC_OK;
210   int i, alt_ref_enabled = 0;
211
212   if (options == NULL) return VPX_CODEC_OK;
213   input_string = strdup(options);
214
215   // parse option name
216   option_name = strtok_r(input_string, "=", &input_ptr);
217   while (option_name != NULL) {
218     // parse option value
219     option_value = strtok_r(NULL, " ", &input_ptr);
220     if (option_value == NULL) {
221       svc_log(svc_ctx, SVC_LOG_ERROR, "option missing value: %s\n",
222               option_name);
223       res = VPX_CODEC_INVALID_PARAM;
224       break;
225     }
226     if (strcmp("spatial-layers", option_name) == 0) {
227       svc_ctx->spatial_layers = atoi(option_value);
228     } else if (strcmp("temporal-layers", option_name) == 0) {
229       svc_ctx->temporal_layers = atoi(option_value);
230     } else if (strcmp("scale-factors", option_name) == 0) {
231       res = parse_layer_options_from_string(svc_ctx, SCALE_FACTOR, option_value,
232                                             si->svc_params.scaling_factor_num,
233                                             si->svc_params.scaling_factor_den);
234       if (res != VPX_CODEC_OK) break;
235     } else if (strcmp("max-quantizers", option_name) == 0) {
236       res = parse_layer_options_from_string(svc_ctx, QUANTIZER, option_value,
237                                             si->svc_params.max_quantizers,
238                                             NULL);
239       if (res != VPX_CODEC_OK) break;
240     } else if (strcmp("min-quantizers", option_name) == 0) {
241       res = parse_layer_options_from_string(svc_ctx, QUANTIZER, option_value,
242                                             si->svc_params.min_quantizers,
243                                             NULL);
244       if (res != VPX_CODEC_OK) break;
245     } else if (strcmp("auto-alt-refs", option_name) == 0) {
246       res = parse_layer_options_from_string(svc_ctx, AUTO_ALT_REF, option_value,
247                                             si->enable_auto_alt_ref, NULL);
248       if (res != VPX_CODEC_OK) break;
249     } else if (strcmp("bitrates", option_name) == 0) {
250       res = parse_layer_options_from_string(svc_ctx, BITRATE, option_value,
251                                             si->bitrates, NULL);
252       if (res != VPX_CODEC_OK) break;
253     } else if (strcmp("multi-frame-contexts", option_name) == 0) {
254       si->use_multiple_frame_contexts = atoi(option_value);
255     } else {
256       svc_log(svc_ctx, SVC_LOG_ERROR, "invalid option: %s\n", option_name);
257       res = VPX_CODEC_INVALID_PARAM;
258       break;
259     }
260     option_name = strtok_r(NULL, "=", &input_ptr);
261   }
262   free(input_string);
263
264   for (i = 0; i < svc_ctx->spatial_layers; ++i) {
265     if (si->svc_params.max_quantizers[i] > MAX_QUANTIZER ||
266         si->svc_params.max_quantizers[i] < 0 ||
267         si->svc_params.min_quantizers[i] > si->svc_params.max_quantizers[i] ||
268         si->svc_params.min_quantizers[i] < 0)
269       res = VPX_CODEC_INVALID_PARAM;
270   }
271
272   if (si->use_multiple_frame_contexts &&
273       (svc_ctx->spatial_layers > 3 ||
274        svc_ctx->spatial_layers * svc_ctx->temporal_layers > 4))
275     res = VPX_CODEC_INVALID_PARAM;
276
277   for (i = 0; i < svc_ctx->spatial_layers; ++i)
278     alt_ref_enabled += si->enable_auto_alt_ref[i];
279   if (alt_ref_enabled > REF_FRAMES - svc_ctx->spatial_layers) {
280     svc_log(svc_ctx, SVC_LOG_ERROR,
281             "svc: auto alt ref: Maxinum %d(REF_FRAMES - layers) layers could"
282             "enabled auto alt reference frame, but % layers are enabled\n",
283             REF_FRAMES - svc_ctx->spatial_layers, alt_ref_enabled);
284     res = VPX_CODEC_INVALID_PARAM;
285   }
286
287   return res;
288 }
289
290 vpx_codec_err_t vpx_svc_set_options(SvcContext *svc_ctx,
291                                     const char *options) {
292   SvcInternal_t *const si = get_svc_internal(svc_ctx);
293   if (svc_ctx == NULL || options == NULL || si == NULL) {
294     return VPX_CODEC_INVALID_PARAM;
295   }
296   strncpy(si->options, options, sizeof(si->options));
297   si->options[sizeof(si->options) - 1] = '\0';
298   return VPX_CODEC_OK;
299 }
300
301 void assign_layer_bitrates(const SvcContext *svc_ctx,
302                            vpx_codec_enc_cfg_t *const enc_cfg) {
303   int i;
304   const SvcInternal_t *const si = get_const_svc_internal(svc_ctx);
305
306   if (si->bitrates[0] != 0) {
307     enc_cfg->rc_target_bitrate = 0;
308     for (i = 0; i < svc_ctx->spatial_layers; ++i) {
309       enc_cfg->ss_target_bitrate[i] = (unsigned int)si->bitrates[i];
310       enc_cfg->rc_target_bitrate += si->bitrates[i];
311     }
312   } else {
313     float total = 0;
314     float alloc_ratio[VPX_SS_MAX_LAYERS] = {0};
315
316     for (i = 0; i < svc_ctx->spatial_layers; ++i) {
317       if (si->svc_params.scaling_factor_den[i] > 0) {
318         alloc_ratio[i] = (float)(si->svc_params.scaling_factor_num[i] * 1.0 /
319                                  si->svc_params.scaling_factor_den[i]);
320
321         alloc_ratio[i] *= alloc_ratio[i];
322         total += alloc_ratio[i];
323       }
324     }
325
326     for (i = 0; i < VPX_SS_MAX_LAYERS; ++i) {
327       if (total > 0) {
328         enc_cfg->ss_target_bitrate[i] = (unsigned int)
329             (enc_cfg->rc_target_bitrate * alloc_ratio[i] / total);
330       }
331     }
332   }
333 }
334
335 vpx_codec_err_t vpx_svc_init(SvcContext *svc_ctx, vpx_codec_ctx_t *codec_ctx,
336                              vpx_codec_iface_t *iface,
337                              vpx_codec_enc_cfg_t *enc_cfg) {
338   vpx_codec_err_t res;
339   int i;
340   SvcInternal_t *const si = get_svc_internal(svc_ctx);
341   if (svc_ctx == NULL || codec_ctx == NULL || iface == NULL ||
342       enc_cfg == NULL) {
343     return VPX_CODEC_INVALID_PARAM;
344   }
345   if (si == NULL) return VPX_CODEC_MEM_ERROR;
346
347   si->codec_ctx = codec_ctx;
348
349   si->width = enc_cfg->g_w;
350   si->height = enc_cfg->g_h;
351
352   if (enc_cfg->kf_max_dist < 2) {
353     svc_log(svc_ctx, SVC_LOG_ERROR, "key frame distance too small: %d\n",
354             enc_cfg->kf_max_dist);
355     return VPX_CODEC_INVALID_PARAM;
356   }
357   si->kf_dist = enc_cfg->kf_max_dist;
358
359   if (svc_ctx->spatial_layers == 0)
360     svc_ctx->spatial_layers = VPX_SS_DEFAULT_LAYERS;
361   if (svc_ctx->spatial_layers < 1 ||
362       svc_ctx->spatial_layers > VPX_SS_MAX_LAYERS) {
363     svc_log(svc_ctx, SVC_LOG_ERROR, "spatial layers: invalid value: %d\n",
364             svc_ctx->spatial_layers);
365     return VPX_CODEC_INVALID_PARAM;
366   }
367
368   for (i = 0; i < VPX_SS_MAX_LAYERS; ++i) {
369     si->svc_params.max_quantizers[i] = MAX_QUANTIZER;
370     si->svc_params.min_quantizers[i] = 0;
371     si->svc_params.scaling_factor_num[i] = DEFAULT_SCALE_FACTORS_NUM[i];
372     si->svc_params.scaling_factor_den[i] = DEFAULT_SCALE_FACTORS_DEN[i];
373   }
374
375   // Parse aggregate command line options. Options must start with
376   // "layers=xx" then followed by other options
377   res = parse_options(svc_ctx, si->options);
378   if (res != VPX_CODEC_OK) return res;
379
380   if (svc_ctx->spatial_layers < 1)
381     svc_ctx->spatial_layers = 1;
382   if (svc_ctx->spatial_layers > VPX_SS_MAX_LAYERS)
383     svc_ctx->spatial_layers = VPX_SS_MAX_LAYERS;
384
385   if (svc_ctx->temporal_layers < 1)
386     svc_ctx->temporal_layers = 1;
387   if (svc_ctx->temporal_layers > VPX_TS_MAX_LAYERS)
388     svc_ctx->temporal_layers = VPX_TS_MAX_LAYERS;
389
390   assign_layer_bitrates(svc_ctx, enc_cfg);
391
392 #if CONFIG_SPATIAL_SVC
393   for (i = 0; i < svc_ctx->spatial_layers; ++i)
394     enc_cfg->ss_enable_auto_alt_ref[i] = si->enable_auto_alt_ref[i];
395 #endif
396
397   if (svc_ctx->temporal_layers > 1) {
398     int i;
399     for (i = 0; i < svc_ctx->temporal_layers; ++i) {
400       enc_cfg->ts_target_bitrate[i] = enc_cfg->rc_target_bitrate /
401                                       svc_ctx->temporal_layers;
402       enc_cfg->ts_rate_decimator[i] = 1 << (svc_ctx->temporal_layers - 1 - i);
403     }
404   }
405
406   // modify encoder configuration
407   enc_cfg->ss_number_layers = svc_ctx->spatial_layers;
408   enc_cfg->ts_number_layers = svc_ctx->temporal_layers;
409
410   if (enc_cfg->g_error_resilient == 0 && si->use_multiple_frame_contexts == 0)
411     enc_cfg->g_error_resilient = 1;
412
413   // Initialize codec
414   res = vpx_codec_enc_init(codec_ctx, iface, enc_cfg, VPX_CODEC_USE_PSNR);
415   if (res != VPX_CODEC_OK) {
416     svc_log(svc_ctx, SVC_LOG_ERROR, "svc_enc_init error\n");
417     return res;
418   }
419
420   vpx_codec_control(codec_ctx, VP9E_SET_SVC, 1);
421   vpx_codec_control(codec_ctx, VP9E_SET_SVC_PARAMETERS, &si->svc_params);
422
423   return VPX_CODEC_OK;
424 }
425
426 /**
427  * Encode a frame into multiple layers
428  * Create a superframe containing the individual layers
429  */
430 vpx_codec_err_t vpx_svc_encode(SvcContext *svc_ctx,
431                                vpx_codec_ctx_t *codec_ctx,
432                                struct vpx_image *rawimg,
433                                vpx_codec_pts_t pts,
434                                int64_t duration, int deadline) {
435   vpx_codec_err_t res;
436   vpx_codec_iter_t iter;
437   const vpx_codec_cx_pkt_t *cx_pkt;
438   SvcInternal_t *const si = get_svc_internal(svc_ctx);
439   if (svc_ctx == NULL || codec_ctx == NULL || si == NULL) {
440     return VPX_CODEC_INVALID_PARAM;
441   }
442
443   svc_log_reset(svc_ctx);
444
445   res = vpx_codec_encode(codec_ctx, rawimg, pts, (uint32_t)duration, 0,
446                          deadline);
447   if (res != VPX_CODEC_OK) {
448     return res;
449   }
450   // save compressed data
451   iter = NULL;
452   while ((cx_pkt = vpx_codec_get_cx_data(codec_ctx, &iter))) {
453     switch (cx_pkt->kind) {
454 #if CONFIG_SPATIAL_SVC
455       case VPX_CODEC_SPATIAL_SVC_LAYER_PSNR: {
456         int i;
457         for (i = 0; i < svc_ctx->spatial_layers; ++i) {
458           int j;
459           svc_log(svc_ctx, SVC_LOG_DEBUG,
460                   "SVC frame: %d, layer: %d, PSNR(Total/Y/U/V): "
461                   "%2.3f  %2.3f  %2.3f  %2.3f \n",
462                   si->psnr_pkt_received, i,
463                   cx_pkt->data.layer_psnr[i].psnr[0],
464                   cx_pkt->data.layer_psnr[i].psnr[1],
465                   cx_pkt->data.layer_psnr[i].psnr[2],
466                   cx_pkt->data.layer_psnr[i].psnr[3]);
467           svc_log(svc_ctx, SVC_LOG_DEBUG,
468                   "SVC frame: %d, layer: %d, SSE(Total/Y/U/V): "
469                   "%2.3f  %2.3f  %2.3f  %2.3f \n",
470                   si->psnr_pkt_received, i,
471                   cx_pkt->data.layer_psnr[i].sse[0],
472                   cx_pkt->data.layer_psnr[i].sse[1],
473                   cx_pkt->data.layer_psnr[i].sse[2],
474                   cx_pkt->data.layer_psnr[i].sse[3]);
475
476           for (j = 0; j < COMPONENTS; ++j) {
477             si->psnr_sum[i][j] +=
478                 cx_pkt->data.layer_psnr[i].psnr[j];
479             si->sse_sum[i][j] += cx_pkt->data.layer_psnr[i].sse[j];
480           }
481         }
482         ++si->psnr_pkt_received;
483         break;
484       }
485       case VPX_CODEC_SPATIAL_SVC_LAYER_SIZES: {
486         int i;
487         for (i = 0; i < svc_ctx->spatial_layers; ++i)
488           si->bytes_sum[i] += cx_pkt->data.layer_sizes[i];
489         break;
490       }
491 #endif
492       default: {
493         break;
494       }
495     }
496   }
497
498   return VPX_CODEC_OK;
499 }
500
501 const char *vpx_svc_get_message(const SvcContext *svc_ctx) {
502   const SvcInternal_t *const si = get_const_svc_internal(svc_ctx);
503   if (svc_ctx == NULL || si == NULL) return NULL;
504   return si->message_buffer;
505 }
506
507 static double calc_psnr(double d) {
508   if (d == 0) return 100;
509   return -10.0 * log(d) / log(10.0);
510 }
511
512 // dump accumulated statistics and reset accumulated values
513 const char *vpx_svc_dump_statistics(SvcContext *svc_ctx) {
514   int number_of_frames;
515   int i, j;
516   uint32_t bytes_total = 0;
517   double scale[COMPONENTS];
518   double psnr[COMPONENTS];
519   double mse[COMPONENTS];
520   double y_scale;
521
522   SvcInternal_t *const si = get_svc_internal(svc_ctx);
523   if (svc_ctx == NULL || si == NULL) return NULL;
524
525   svc_log_reset(svc_ctx);
526
527   number_of_frames = si->psnr_pkt_received;
528   if (number_of_frames <= 0) return vpx_svc_get_message(svc_ctx);
529
530   svc_log(svc_ctx, SVC_LOG_INFO, "\n");
531   for (i = 0; i < svc_ctx->spatial_layers; ++i) {
532
533     svc_log(svc_ctx, SVC_LOG_INFO,
534             "Layer %d Average PSNR=[%2.3f, %2.3f, %2.3f, %2.3f], Bytes=[%u]\n",
535             i, (double)si->psnr_sum[i][0] / number_of_frames,
536             (double)si->psnr_sum[i][1] / number_of_frames,
537             (double)si->psnr_sum[i][2] / number_of_frames,
538             (double)si->psnr_sum[i][3] / number_of_frames, si->bytes_sum[i]);
539     // the following psnr calculation is deduced from ffmpeg.c#print_report
540     y_scale = si->width * si->height * 255.0 * 255.0 * number_of_frames;
541     scale[1] = y_scale;
542     scale[2] = scale[3] = y_scale / 4;  // U or V
543     scale[0] = y_scale * 1.5;           // total
544
545     for (j = 0; j < COMPONENTS; j++) {
546       psnr[j] = calc_psnr(si->sse_sum[i][j] / scale[j]);
547       mse[j] = si->sse_sum[i][j] * 255.0 * 255.0 / scale[j];
548     }
549     svc_log(svc_ctx, SVC_LOG_INFO,
550             "Layer %d Overall PSNR=[%2.3f, %2.3f, %2.3f, %2.3f]\n", i, psnr[0],
551             psnr[1], psnr[2], psnr[3]);
552     svc_log(svc_ctx, SVC_LOG_INFO,
553             "Layer %d Overall MSE=[%2.3f, %2.3f, %2.3f, %2.3f]\n", i, mse[0],
554             mse[1], mse[2], mse[3]);
555
556     bytes_total += si->bytes_sum[i];
557     // clear sums for next time
558     si->bytes_sum[i] = 0;
559     for (j = 0; j < COMPONENTS; ++j) {
560       si->psnr_sum[i][j] = 0;
561       si->sse_sum[i][j] = 0;
562     }
563   }
564
565   // only display statistics once
566   si->psnr_pkt_received = 0;
567
568   svc_log(svc_ctx, SVC_LOG_INFO, "Total Bytes=[%u]\n", bytes_total);
569   return vpx_svc_get_message(svc_ctx);
570 }
571
572 void vpx_svc_release(SvcContext *svc_ctx) {
573   SvcInternal_t *si;
574   if (svc_ctx == NULL) return;
575   // do not use get_svc_internal as it will unnecessarily allocate an
576   // SvcInternal_t if it was not already allocated
577   si = (SvcInternal_t *)svc_ctx->internal;
578   if (si != NULL) {
579     free(si);
580     svc_ctx->internal = NULL;
581   }
582 }
583