// SPDX-License-Identifier: GPL-2.0 /* * Copyright (C) 2019 Pengutronix, Michael Tretter <[email protected]> * * Convert NAL units between raw byte sequence payloads (RBSP) and C structs * * The conversion is defined in "ITU-T Rec. H.264 (04/2017) Advanced video * coding for generic audiovisual services". Decoder drivers may use the * parser to parse RBSP from encoded streams and configure the hardware, if * the hardware is not able to parse RBSP itself. Encoder drivers may use the * generator to generate the RBSP for SPS/PPS nal units and add them to the * encoded stream if the hardware does not generate the units. */ #include <linux/kernel.h> #include <linux/types.h> #include <linux/string.h> #include <linux/v4l2-controls.h> #include <linux/device.h> #include <linux/export.h> #include <linux/log2.h> #include "nal-h264.h" #include "nal-rbsp.h" /* * See Rec. ITU-T H.264 (04/2017) Table 7-1 - NAL unit type codes, syntax * element categories, and NAL unit type classes */ enum nal_unit_type { … }; static void nal_h264_write_start_code_prefix(struct rbsp *rbsp) { … } static void nal_h264_read_start_code_prefix(struct rbsp *rbsp) { … } static void nal_h264_write_filler_data(struct rbsp *rbsp) { … } static void nal_h264_read_filler_data(struct rbsp *rbsp) { … } static void nal_h264_rbsp_hrd_parameters(struct rbsp *rbsp, struct nal_h264_hrd_parameters *hrd) { … } static void nal_h264_rbsp_vui_parameters(struct rbsp *rbsp, struct nal_h264_vui_parameters *vui) { … } static void nal_h264_rbsp_sps(struct rbsp *rbsp, struct nal_h264_sps *sps) { … } static void nal_h264_rbsp_pps(struct rbsp *rbsp, struct nal_h264_pps *pps) { … } /** * nal_h264_write_sps() - Write SPS NAL unit into RBSP format * @dev: device pointer * @dest: the buffer that is filled with RBSP data * @n: maximum size of @dest in bytes * @sps: &struct nal_h264_sps to convert to RBSP * * Convert @sps to RBSP data and write it into @dest. * * The size of the SPS NAL unit is not known in advance and this function will * fail, if @dest does not hold sufficient space for the SPS NAL unit. * * Return: number of bytes written to @dest or negative error code */ ssize_t nal_h264_write_sps(const struct device *dev, void *dest, size_t n, struct nal_h264_sps *sps) { … } EXPORT_SYMBOL_GPL(…); /** * nal_h264_read_sps() - Read SPS NAL unit from RBSP format * @dev: device pointer * @sps: the &struct nal_h264_sps to fill from the RBSP data * @src: the buffer that contains the RBSP data * @n: size of @src in bytes * * Read RBSP data from @src and use it to fill @sps. * * Return: number of bytes read from @src or negative error code */ ssize_t nal_h264_read_sps(const struct device *dev, struct nal_h264_sps *sps, void *src, size_t n) { … } EXPORT_SYMBOL_GPL(…); /** * nal_h264_write_pps() - Write PPS NAL unit into RBSP format * @dev: device pointer * @dest: the buffer that is filled with RBSP data * @n: maximum size of @dest in bytes * @pps: &struct nal_h264_pps to convert to RBSP * * Convert @pps to RBSP data and write it into @dest. * * The size of the PPS NAL unit is not known in advance and this function will * fail, if @dest does not hold sufficient space for the PPS NAL unit. * * Return: number of bytes written to @dest or negative error code */ ssize_t nal_h264_write_pps(const struct device *dev, void *dest, size_t n, struct nal_h264_pps *pps) { … } EXPORT_SYMBOL_GPL(…); /** * nal_h264_read_pps() - Read PPS NAL unit from RBSP format * @dev: device pointer * @pps: the &struct nal_h264_pps to fill from the RBSP data * @src: the buffer that contains the RBSP data * @n: size of @src in bytes * * Read RBSP data from @src and use it to fill @pps. * * Return: number of bytes read from @src or negative error code */ ssize_t nal_h264_read_pps(const struct device *dev, struct nal_h264_pps *pps, void *src, size_t n) { … } EXPORT_SYMBOL_GPL(…); /** * nal_h264_write_filler() - Write filler data RBSP * @dev: device pointer * @dest: buffer to fill with filler data * @n: size of the buffer to fill with filler data * * Write a filler data RBSP to @dest with a size of @n bytes and return the * number of written filler data bytes. * * Use this function to generate dummy data in an RBSP data stream that can be * safely ignored by h264 decoders. * * The RBSP format of the filler data is specified in Rec. ITU-T H.264 * (04/2017) 7.3.2.7 Filler data RBSP syntax. * * Return: number of filler data bytes (including marker) or negative error */ ssize_t nal_h264_write_filler(const struct device *dev, void *dest, size_t n) { … } EXPORT_SYMBOL_GPL(…); /** * nal_h264_read_filler() - Read filler data RBSP * @dev: device pointer * @src: buffer with RBSP data that is read * @n: maximum size of src that shall be read * * Read a filler data RBSP from @src up to a maximum size of @n bytes and * return the size of the filler data in bytes including the marker. * * This function is used to parse filler data and skip the respective bytes in * the RBSP data. * * The RBSP format of the filler data is specified in Rec. ITU-T H.264 * (04/2017) 7.3.2.7 Filler data RBSP syntax. * * Return: number of filler data bytes (including marker) or negative error */ ssize_t nal_h264_read_filler(const struct device *dev, void *src, size_t n) { … } EXPORT_SYMBOL_GPL(…);