📄 yuv4mpeg.c
字号:
/* * yuv4mpeg.c: Functions for reading and writing "new" YUV4MPEG streams * * Copyright (C) 2001 Matthew J. Marjanovic <maddog@mir.com> * * * This program is free software; you can redistribute it and/or * modify it under the terms of the GNU General Public License * as published by the Free Software Foundation; either version 2 * of the License, or (at your option) any later version. * * This program 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 General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program; if not, write to the Free Software * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. * */#include <config.h>#include <unistd.h>#include <stdio.h>#include <stdlib.h>#include <string.h>#define INTERNAL_Y4M_LIBCODE_STUFF_QPX#include "yuv4mpeg.h"#include "yuv4mpeg_intern.h"#include "mjpeg_logging.h"static int _y4mparam_allow_unknown_tags = 1; /* default is forgiveness */static int _y4mparam_feature_level = 0; /* default is ol YUV4MPEG2 */static void *(*_y4m_alloc)(size_t bytes) = malloc;static void (*_y4m_free)(void *ptr) = free;int y4m_allow_unknown_tags(int yn){ int old = _y4mparam_allow_unknown_tags; if (yn >= 0) _y4mparam_allow_unknown_tags = (yn) ? 1 : 0; return old;}int y4m_accept_extensions(int level){ int old = _y4mparam_feature_level; if (level >= 0) _y4mparam_feature_level = level; return old;}/************************************************************************* * * Convenience functions for fd read/write * * - guaranteed to transfer entire payload (or fail) * - returns: * 0 on complete success * +(# of remaining bytes) on eof (for y4m_read) * -(# of rem. bytes) on error (and ERRNO should be set) * *************************************************************************/ssize_t y4m_read(int fd, void *buf, size_t len){ ssize_t n; uint8_t *ptr = (uint8_t *)buf; while (len > 0) { n = read(fd, ptr, len); if (n <= 0) { /* return amount left to read */ if (n == 0) return len; /* n == 0 --> eof */ else return -len; /* n < 0 --> error */ } ptr += n; len -= n; } return 0;}ssize_t y4m_write(int fd, const void *buf, size_t len){ ssize_t n; const uint8_t *ptr = (const uint8_t *)buf; while (len > 0) { n = write(fd, ptr, len); if (n <= 0) return -len; /* return amount left to write */ ptr += n; len -= n; } return 0;}/************************************************************************* * * "Extra tags" handling * *************************************************************************/static char *y4m_new_xtag(void){ return _y4m_alloc(Y4M_MAX_XTAG_SIZE * sizeof(char));}void y4m_init_xtag_list(y4m_xtag_list_t *xtags){ int i; xtags->count = 0; for (i = 0; i < Y4M_MAX_XTAGS; i++) { xtags->tags[i] = NULL; }}void y4m_fini_xtag_list(y4m_xtag_list_t *xtags){ int i; for (i = 0; i < Y4M_MAX_XTAGS; i++) { if (xtags->tags[i] != NULL) { _y4m_free(xtags->tags[i]); xtags->tags[i] = NULL; } } xtags->count = 0;}void y4m_copy_xtag_list(y4m_xtag_list_t *dest, const y4m_xtag_list_t *src){ int i; for (i = 0; i < src->count; i++) { if (dest->tags[i] == NULL) dest->tags[i] = y4m_new_xtag(); strncpy(dest->tags[i], src->tags[i], Y4M_MAX_XTAG_SIZE); } dest->count = src->count;}static int y4m_snprint_xtags(char *s, int maxn, const y4m_xtag_list_t *xtags){ int i, room; for (i = 0, room = maxn - 1; i < xtags->count; i++) { int n = snprintf(s, room + 1, " %s", xtags->tags[i]); if ((n < 0) || (n > room)) return Y4M_ERR_HEADER; s += n; room -= n; } s[0] = '\n'; /* finish off header with newline */ s[1] = '\0'; /* ...and end-of-string */ return Y4M_OK;}int y4m_xtag_count(const y4m_xtag_list_t *xtags){ return xtags->count;}const char *y4m_xtag_get(const y4m_xtag_list_t *xtags, int n){ if (n >= xtags->count) return NULL; else return xtags->tags[n];}int y4m_xtag_add(y4m_xtag_list_t *xtags, const char *tag){ if (xtags->count >= Y4M_MAX_XTAGS) return Y4M_ERR_XXTAGS; if (xtags->tags[xtags->count] == NULL) xtags->tags[xtags->count] = y4m_new_xtag(); strncpy(xtags->tags[xtags->count], tag, Y4M_MAX_XTAG_SIZE); (xtags->count)++; return Y4M_OK;}int y4m_xtag_remove(y4m_xtag_list_t *xtags, int n){ int i; char *q; if ((n < 0) || (n >= xtags->count)) return Y4M_ERR_RANGE; q = xtags->tags[n]; for (i = n; i < (xtags->count - 1); i++) xtags->tags[i] = xtags->tags[i+1]; xtags->tags[i] = q; (xtags->count)--; return Y4M_OK;}int y4m_xtag_clearlist(y4m_xtag_list_t *xtags){ xtags->count = 0; return Y4M_OK;}int y4m_xtag_addlist(y4m_xtag_list_t *dest, const y4m_xtag_list_t *src){ int i, j; if ((dest->count + src->count) > Y4M_MAX_XTAGS) return Y4M_ERR_XXTAGS; for (i = dest->count, j = 0; j < src->count; i++, j++) { if (dest->tags[i] == NULL) dest->tags[i] = y4m_new_xtag(); strncpy(dest->tags[i], src->tags[i], Y4M_MAX_XTAG_SIZE); } dest->count += src->count; return Y4M_OK;} /************************************************************************* * * Creators/destructors for y4m_*_info_t structures * *************************************************************************/void y4m_init_stream_info(y4m_stream_info_t *info){ if (info == NULL) return; /* init substructures */ y4m_init_xtag_list(&(info->x_tags)); /* set defaults */ y4m_clear_stream_info(info);}void y4m_clear_stream_info(y4m_stream_info_t *info){ if (info == NULL) return; /* clear/initialize info */ info->width = Y4M_UNKNOWN; info->height = Y4M_UNKNOWN; info->interlace = Y4M_UNKNOWN; info->framerate = y4m_fps_UNKNOWN; info->sampleaspect = y4m_sar_UNKNOWN; if (_y4mparam_feature_level < 1) { info->chroma = Y4M_CHROMA_420JPEG; } else { info->chroma = Y4M_UNKNOWN; } y4m_xtag_clearlist(&(info->x_tags));}void y4m_copy_stream_info(y4m_stream_info_t *dest, const y4m_stream_info_t *src){ if ((dest == NULL) || (src == NULL)) return; /* copy info */ dest->width = src->width; dest->height = src->height; dest->interlace = src->interlace; dest->framerate = src->framerate; dest->sampleaspect = src->sampleaspect; dest->chroma = src->chroma; y4m_copy_xtag_list(&(dest->x_tags), &(src->x_tags));}void y4m_fini_stream_info(y4m_stream_info_t *info){ if (info == NULL) return; y4m_fini_xtag_list(&(info->x_tags));}void y4m_si_set_width(y4m_stream_info_t *si, int width){ si->width = width;}int y4m_si_get_width(const y4m_stream_info_t *si){ return si->width; }void y4m_si_set_height(y4m_stream_info_t *si, int height){ si->height = height; }int y4m_si_get_height(const y4m_stream_info_t *si){ return si->height; }void y4m_si_set_interlace(y4m_stream_info_t *si, int interlace){ si->interlace = interlace; }int y4m_si_get_interlace(const y4m_stream_info_t *si){ return si->interlace; }void y4m_si_set_framerate(y4m_stream_info_t *si, y4m_ratio_t framerate){ si->framerate = framerate; }y4m_ratio_t y4m_si_get_framerate(const y4m_stream_info_t *si){ return si->framerate; }void y4m_si_set_sampleaspect(y4m_stream_info_t *si, y4m_ratio_t sar){ si->sampleaspect = sar; }y4m_ratio_t y4m_si_get_sampleaspect(const y4m_stream_info_t *si){ return si->sampleaspect; }void y4m_si_set_chroma(y4m_stream_info_t *si, int chroma_mode){ si->chroma = chroma_mode; }int y4m_si_get_chroma(const y4m_stream_info_t *si){ return si->chroma; }int y4m_si_get_plane_count(const y4m_stream_info_t *si){ switch (si->chroma) { case Y4M_CHROMA_420JPEG: case Y4M_CHROMA_420MPEG2: case Y4M_CHROMA_420PALDV: case Y4M_CHROMA_444: case Y4M_CHROMA_422: case Y4M_CHROMA_411: return 3; case Y4M_CHROMA_MONO: return 1; case Y4M_CHROMA_444ALPHA: return 4; default: return Y4M_UNKNOWN; }}int y4m_si_get_plane_width(const y4m_stream_info_t *si, int plane){ switch (plane) { case 0: return (si->width); case 1: case 2: switch (si->chroma) { case Y4M_CHROMA_420JPEG: case Y4M_CHROMA_420MPEG2: case Y4M_CHROMA_420PALDV: return (si->width) / 2; case Y4M_CHROMA_444: case Y4M_CHROMA_444ALPHA: return (si->width); case Y4M_CHROMA_422: return (si->width) / 2; case Y4M_CHROMA_411: return (si->width) / 4; default: return Y4M_UNKNOWN; } case 3: switch (si->chroma) { case Y4M_CHROMA_444ALPHA: return (si->width); default: return Y4M_UNKNOWN; } default: return Y4M_UNKNOWN; }}int y4m_si_get_plane_height(const y4m_stream_info_t *si, int plane){ switch (plane) { case 0: return (si->height); case 1: case 2: switch (si->chroma) { case Y4M_CHROMA_420JPEG: case Y4M_CHROMA_420MPEG2: case Y4M_CHROMA_420PALDV: return (si->height) / 2; case Y4M_CHROMA_444: case Y4M_CHROMA_444ALPHA: case Y4M_CHROMA_422: case Y4M_CHROMA_411: return (si->height); default: return Y4M_UNKNOWN; } case 3: switch (si->chroma) { case Y4M_CHROMA_444ALPHA: return (si->height); default: return Y4M_UNKNOWN; } default: return Y4M_UNKNOWN; }}int y4m_si_get_plane_length(const y4m_stream_info_t *si, int plane){ int w = y4m_si_get_plane_width(si, plane); int h = y4m_si_get_plane_height(si, plane); if ((w != Y4M_UNKNOWN) && (h != Y4M_UNKNOWN)) return (w * h); else return Y4M_UNKNOWN;}int y4m_si_get_framelength(const y4m_stream_info_t *si){ int total = 0; int planes = y4m_si_get_plane_count(si); int p; for (p = 0; p < planes; p++) { int plen = y4m_si_get_plane_length(si, p); if (plen == Y4M_UNKNOWN) return Y4M_UNKNOWN; total += plen; } return total;}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -