summaryrefslogtreecommitdiff
path: root/libavcodec/vaapi_encode_h26x.c
diff options
context:
space:
mode:
authorDerek Buitenhuis <derek.buitenhuis@gmail.com>2016-05-08 22:43:52 +0100
committerDerek Buitenhuis <derek.buitenhuis@gmail.com>2016-05-08 22:43:52 +0100
commitb975aeec02e4518a4e5f2996cf51d113e42e1727 (patch)
tree2a2db1597eb4a20f4ad9f7be56d3992bbcfb8448 /libavcodec/vaapi_encode_h26x.c
parent508957fd0fc398271f3ec87cde63ba4533678bf9 (diff)
parent2c62fcdf5d617791a653d7957d449f75569eede0 (diff)
Merge commit '2c62fcdf5d617791a653d7957d449f75569eede0'
* commit '2c62fcdf5d617791a653d7957d449f75569eede0': lavc: VAAPI H.264 encoder Merged-by: Derek Buitenhuis <derek.buitenhuis@gmail.com>
Diffstat (limited to 'libavcodec/vaapi_encode_h26x.c')
-rw-r--r--libavcodec/vaapi_encode_h26x.c68
1 files changed, 68 insertions, 0 deletions
diff --git a/libavcodec/vaapi_encode_h26x.c b/libavcodec/vaapi_encode_h26x.c
new file mode 100644
index 0000000000..d806f9b52b
--- /dev/null
+++ b/libavcodec/vaapi_encode_h26x.c
@@ -0,0 +1,68 @@
+/*
+ * This file is part of FFmpeg.
+ *
+ * FFmpeg is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2.1 of the License, or (at your option) any later version.
+ *
+ * FFmpeg is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with FFmpeg; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
+ */
+
+#include <stdint.h>
+
+#include "vaapi_encode_h26x.h"
+
+int ff_vaapi_encode_h26x_nal_unit_to_byte_stream(uint8_t *dst, size_t *dst_bit_len,
+ uint8_t *src, size_t src_bit_len)
+{
+ size_t dp, sp;
+ int zero_run = 0;
+ size_t dst_len = *dst_bit_len / 8;
+ size_t src_len = (src_bit_len + 7) / 8;
+ int trailing_zeroes = src_len * 8 - src_bit_len;
+
+ if (dst_len < src_len + 4) {
+ // Definitely doesn't fit.
+ goto fail;
+ }
+
+ // Start code.
+ dst[0] = dst[1] = dst[2] = 0;
+ dst[3] = 1;
+ dp = 4;
+
+ for (sp = 0; sp < src_len; sp++) {
+ if (dp >= dst_len)
+ goto fail;
+ if (zero_run < 2) {
+ if (src[sp] == 0)
+ ++zero_run;
+ else
+ zero_run = 0;
+ } else {
+ if ((src[sp] & ~3) == 0) {
+ // emulation_prevention_three_byte
+ dst[dp++] = 3;
+ if (dp >= dst_len)
+ goto fail;
+ }
+ zero_run = src[sp] == 0;
+ }
+ dst[dp++] = src[sp];
+ }
+
+ *dst_bit_len = 8 * dp - trailing_zeroes;
+ return 0;
+
+fail:
+ *dst_bit_len = 0;
+ return AVERROR(ENOSPC);
+}