You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

357 lines
14KB

  1. /*
  2. * YUV4MPEG demuxer
  3. * Copyright (c) 2001, 2002, 2003 Fabrice Bellard
  4. *
  5. * This file is part of FFmpeg.
  6. *
  7. * FFmpeg is free software; you can redistribute it and/or
  8. * modify it under the terms of the GNU Lesser General Public
  9. * License as published by the Free Software Foundation; either
  10. * version 2.1 of the License, or (at your option) any later version.
  11. *
  12. * FFmpeg is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  15. * Lesser General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU Lesser General Public
  18. * License along with FFmpeg; if not, write to the Free Software
  19. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  20. */
  21. #include "libavutil/imgutils.h"
  22. #include "avformat.h"
  23. #include "internal.h"
  24. #include "yuv4mpeg.h"
  25. /* Header size increased to allow room for optional flags */
  26. #define MAX_YUV4_HEADER 96
  27. #define MAX_FRAME_HEADER 80
  28. static int yuv4_read_header(AVFormatContext *s)
  29. {
  30. char header[MAX_YUV4_HEADER + 10]; // Include headroom for
  31. // the longest option
  32. char *tokstart, *tokend, *header_end;
  33. int i;
  34. AVIOContext *pb = s->pb;
  35. int width = -1, height = -1, raten = 0,
  36. rated = 0, aspectn = 0, aspectd = 0;
  37. enum AVPixelFormat pix_fmt = AV_PIX_FMT_NONE, alt_pix_fmt = AV_PIX_FMT_NONE;
  38. enum AVChromaLocation chroma_sample_location = AVCHROMA_LOC_UNSPECIFIED;
  39. enum AVFieldOrder field_order = AV_FIELD_UNKNOWN;
  40. enum AVColorRange color_range = AVCOL_RANGE_UNSPECIFIED;
  41. AVStream *st;
  42. for (i = 0; i < MAX_YUV4_HEADER; i++) {
  43. header[i] = avio_r8(pb);
  44. if (header[i] == '\n') {
  45. header[i + 1] = 0x20; // Add a space after last option.
  46. // Makes parsing "444" vs "444alpha" easier.
  47. header[i + 2] = 0;
  48. break;
  49. }
  50. }
  51. if (i == MAX_YUV4_HEADER) {
  52. av_log(s, AV_LOG_ERROR, "Header too large.\n");
  53. return AVERROR(EINVAL);
  54. }
  55. if (strncmp(header, Y4M_MAGIC, strlen(Y4M_MAGIC))) {
  56. av_log(s, AV_LOG_ERROR, "Invalid magic number for yuv4mpeg.\n");
  57. return AVERROR(EINVAL);
  58. }
  59. header_end = &header[i + 1]; // Include space
  60. for (tokstart = &header[strlen(Y4M_MAGIC) + 1];
  61. tokstart < header_end; tokstart++) {
  62. if (*tokstart == 0x20)
  63. continue;
  64. switch (*tokstart++) {
  65. case 'W': // Width. Required.
  66. width = strtol(tokstart, &tokend, 10);
  67. tokstart = tokend;
  68. break;
  69. case 'H': // Height. Required.
  70. height = strtol(tokstart, &tokend, 10);
  71. tokstart = tokend;
  72. break;
  73. case 'C': // Color space
  74. if (strncmp("420jpeg", tokstart, 7) == 0) {
  75. pix_fmt = AV_PIX_FMT_YUV420P;
  76. chroma_sample_location = AVCHROMA_LOC_CENTER;
  77. } else if (strncmp("420mpeg2", tokstart, 8) == 0) {
  78. pix_fmt = AV_PIX_FMT_YUV420P;
  79. chroma_sample_location = AVCHROMA_LOC_LEFT;
  80. } else if (strncmp("420paldv", tokstart, 8) == 0) {
  81. pix_fmt = AV_PIX_FMT_YUV420P;
  82. chroma_sample_location = AVCHROMA_LOC_TOPLEFT;
  83. } else if (strncmp("420p16", tokstart, 6) == 0) {
  84. pix_fmt = AV_PIX_FMT_YUV420P16;
  85. } else if (strncmp("422p16", tokstart, 6) == 0) {
  86. pix_fmt = AV_PIX_FMT_YUV422P16;
  87. } else if (strncmp("444p16", tokstart, 6) == 0) {
  88. pix_fmt = AV_PIX_FMT_YUV444P16;
  89. } else if (strncmp("420p14", tokstart, 6) == 0) {
  90. pix_fmt = AV_PIX_FMT_YUV420P14;
  91. } else if (strncmp("422p14", tokstart, 6) == 0) {
  92. pix_fmt = AV_PIX_FMT_YUV422P14;
  93. } else if (strncmp("444p14", tokstart, 6) == 0) {
  94. pix_fmt = AV_PIX_FMT_YUV444P14;
  95. } else if (strncmp("420p12", tokstart, 6) == 0) {
  96. pix_fmt = AV_PIX_FMT_YUV420P12;
  97. } else if (strncmp("422p12", tokstart, 6) == 0) {
  98. pix_fmt = AV_PIX_FMT_YUV422P12;
  99. } else if (strncmp("444p12", tokstart, 6) == 0) {
  100. pix_fmt = AV_PIX_FMT_YUV444P12;
  101. } else if (strncmp("420p10", tokstart, 6) == 0) {
  102. pix_fmt = AV_PIX_FMT_YUV420P10;
  103. } else if (strncmp("422p10", tokstart, 6) == 0) {
  104. pix_fmt = AV_PIX_FMT_YUV422P10;
  105. } else if (strncmp("444p10", tokstart, 6) == 0) {
  106. pix_fmt = AV_PIX_FMT_YUV444P10;
  107. } else if (strncmp("420p9", tokstart, 5) == 0) {
  108. pix_fmt = AV_PIX_FMT_YUV420P9;
  109. } else if (strncmp("422p9", tokstart, 5) == 0) {
  110. pix_fmt = AV_PIX_FMT_YUV422P9;
  111. } else if (strncmp("444p9", tokstart, 5) == 0) {
  112. pix_fmt = AV_PIX_FMT_YUV444P9;
  113. } else if (strncmp("420", tokstart, 3) == 0) {
  114. pix_fmt = AV_PIX_FMT_YUV420P;
  115. chroma_sample_location = AVCHROMA_LOC_CENTER;
  116. } else if (strncmp("411", tokstart, 3) == 0) {
  117. pix_fmt = AV_PIX_FMT_YUV411P;
  118. } else if (strncmp("422", tokstart, 3) == 0) {
  119. pix_fmt = AV_PIX_FMT_YUV422P;
  120. } else if (strncmp("444alpha", tokstart, 8) == 0 ) {
  121. pix_fmt = AV_PIX_FMT_YUVA444P;
  122. } else if (strncmp("444", tokstart, 3) == 0) {
  123. pix_fmt = AV_PIX_FMT_YUV444P;
  124. } else if (strncmp("mono16", tokstart, 6) == 0) {
  125. pix_fmt = AV_PIX_FMT_GRAY16;
  126. } else if (strncmp("mono12", tokstart, 6) == 0) {
  127. pix_fmt = AV_PIX_FMT_GRAY12;
  128. } else if (strncmp("mono10", tokstart, 6) == 0) {
  129. pix_fmt = AV_PIX_FMT_GRAY10;
  130. } else if (strncmp("mono9", tokstart, 5) == 0) {
  131. pix_fmt = AV_PIX_FMT_GRAY9;
  132. } else if (strncmp("mono", tokstart, 4) == 0) {
  133. pix_fmt = AV_PIX_FMT_GRAY8;
  134. } else {
  135. av_log(s, AV_LOG_ERROR, "YUV4MPEG stream contains an unknown "
  136. "pixel format.\n");
  137. return AVERROR_INVALIDDATA;
  138. }
  139. while (tokstart < header_end && *tokstart != 0x20)
  140. tokstart++;
  141. break;
  142. case 'I': // Interlace type
  143. switch (*tokstart++){
  144. case '?':
  145. field_order = AV_FIELD_UNKNOWN;
  146. break;
  147. case 'p':
  148. field_order = AV_FIELD_PROGRESSIVE;
  149. break;
  150. case 't':
  151. field_order = AV_FIELD_TT;
  152. break;
  153. case 'b':
  154. field_order = AV_FIELD_BB;
  155. break;
  156. case 'm':
  157. av_log(s, AV_LOG_ERROR, "YUV4MPEG stream contains mixed "
  158. "interlaced and non-interlaced frames.\n");
  159. default:
  160. av_log(s, AV_LOG_ERROR, "YUV4MPEG has invalid header.\n");
  161. return AVERROR(EINVAL);
  162. }
  163. break;
  164. case 'F': // Frame rate
  165. sscanf(tokstart, "%d:%d", &raten, &rated); // 0:0 if unknown
  166. while (tokstart < header_end && *tokstart != 0x20)
  167. tokstart++;
  168. break;
  169. case 'A': // Pixel aspect
  170. sscanf(tokstart, "%d:%d", &aspectn, &aspectd); // 0:0 if unknown
  171. while (tokstart < header_end && *tokstart != 0x20)
  172. tokstart++;
  173. break;
  174. case 'X': // Vendor extensions
  175. if (strncmp("YSCSS=", tokstart, 6) == 0) {
  176. // Older nonstandard pixel format representation
  177. tokstart += 6;
  178. if (strncmp("420JPEG", tokstart, 7) == 0)
  179. alt_pix_fmt = AV_PIX_FMT_YUV420P;
  180. else if (strncmp("420MPEG2", tokstart, 8) == 0)
  181. alt_pix_fmt = AV_PIX_FMT_YUV420P;
  182. else if (strncmp("420PALDV", tokstart, 8) == 0)
  183. alt_pix_fmt = AV_PIX_FMT_YUV420P;
  184. else if (strncmp("420P9", tokstart, 5) == 0)
  185. alt_pix_fmt = AV_PIX_FMT_YUV420P9;
  186. else if (strncmp("422P9", tokstart, 5) == 0)
  187. alt_pix_fmt = AV_PIX_FMT_YUV422P9;
  188. else if (strncmp("444P9", tokstart, 5) == 0)
  189. alt_pix_fmt = AV_PIX_FMT_YUV444P9;
  190. else if (strncmp("420P10", tokstart, 6) == 0)
  191. alt_pix_fmt = AV_PIX_FMT_YUV420P10;
  192. else if (strncmp("422P10", tokstart, 6) == 0)
  193. alt_pix_fmt = AV_PIX_FMT_YUV422P10;
  194. else if (strncmp("444P10", tokstart, 6) == 0)
  195. alt_pix_fmt = AV_PIX_FMT_YUV444P10;
  196. else if (strncmp("420P12", tokstart, 6) == 0)
  197. alt_pix_fmt = AV_PIX_FMT_YUV420P12;
  198. else if (strncmp("422P12", tokstart, 6) == 0)
  199. alt_pix_fmt = AV_PIX_FMT_YUV422P12;
  200. else if (strncmp("444P12", tokstart, 6) == 0)
  201. alt_pix_fmt = AV_PIX_FMT_YUV444P12;
  202. else if (strncmp("420P14", tokstart, 6) == 0)
  203. alt_pix_fmt = AV_PIX_FMT_YUV420P14;
  204. else if (strncmp("422P14", tokstart, 6) == 0)
  205. alt_pix_fmt = AV_PIX_FMT_YUV422P14;
  206. else if (strncmp("444P14", tokstart, 6) == 0)
  207. alt_pix_fmt = AV_PIX_FMT_YUV444P14;
  208. else if (strncmp("420P16", tokstart, 6) == 0)
  209. alt_pix_fmt = AV_PIX_FMT_YUV420P16;
  210. else if (strncmp("422P16", tokstart, 6) == 0)
  211. alt_pix_fmt = AV_PIX_FMT_YUV422P16;
  212. else if (strncmp("444P16", tokstart, 6) == 0)
  213. alt_pix_fmt = AV_PIX_FMT_YUV444P16;
  214. else if (strncmp("411", tokstart, 3) == 0)
  215. alt_pix_fmt = AV_PIX_FMT_YUV411P;
  216. else if (strncmp("422", tokstart, 3) == 0)
  217. alt_pix_fmt = AV_PIX_FMT_YUV422P;
  218. else if (strncmp("444", tokstart, 3) == 0)
  219. alt_pix_fmt = AV_PIX_FMT_YUV444P;
  220. } else if (strncmp("COLORRANGE=", tokstart, 11) == 0) {
  221. tokstart += 11;
  222. if (strncmp("FULL",tokstart, 4) == 0)
  223. color_range = AVCOL_RANGE_JPEG;
  224. else if (strncmp("LIMITED", tokstart, 7) == 0)
  225. color_range = AVCOL_RANGE_MPEG;
  226. }
  227. while (tokstart < header_end && *tokstart != 0x20)
  228. tokstart++;
  229. break;
  230. }
  231. }
  232. if (width == -1 || height == -1) {
  233. av_log(s, AV_LOG_ERROR, "YUV4MPEG has invalid header.\n");
  234. return AVERROR_INVALIDDATA;
  235. }
  236. if (pix_fmt == AV_PIX_FMT_NONE) {
  237. if (alt_pix_fmt == AV_PIX_FMT_NONE)
  238. pix_fmt = AV_PIX_FMT_YUV420P;
  239. else
  240. pix_fmt = alt_pix_fmt;
  241. }
  242. if (raten <= 0 || rated <= 0) {
  243. // Frame rate unknown
  244. raten = 25;
  245. rated = 1;
  246. }
  247. if (aspectn == 0 && aspectd == 0) {
  248. // Pixel aspect unknown
  249. aspectd = 1;
  250. }
  251. st = avformat_new_stream(s, NULL);
  252. if (!st)
  253. return AVERROR(ENOMEM);
  254. st->codecpar->width = width;
  255. st->codecpar->height = height;
  256. av_reduce(&raten, &rated, raten, rated, (1UL << 31) - 1);
  257. avpriv_set_pts_info(st, 64, rated, raten);
  258. st->avg_frame_rate = av_inv_q(st->time_base);
  259. st->codecpar->format = pix_fmt;
  260. st->codecpar->codec_type = AVMEDIA_TYPE_VIDEO;
  261. st->codecpar->codec_id = AV_CODEC_ID_RAWVIDEO;
  262. st->sample_aspect_ratio = (AVRational){ aspectn, aspectd };
  263. st->codecpar->chroma_location = chroma_sample_location;
  264. st->codecpar->color_range = color_range;
  265. st->codecpar->field_order = field_order;
  266. s->packet_size = av_image_get_buffer_size(st->codecpar->format, width, height, 1) + Y4M_FRAME_MAGIC_LEN;
  267. if ((int) s->packet_size < 0)
  268. return s->packet_size;
  269. s->internal->data_offset = avio_tell(pb);
  270. st->duration = (avio_size(pb) - avio_tell(pb)) / s->packet_size;
  271. return 0;
  272. }
  273. static int yuv4_read_packet(AVFormatContext *s, AVPacket *pkt)
  274. {
  275. int i;
  276. char header[MAX_FRAME_HEADER+1];
  277. int ret;
  278. int64_t off = avio_tell(s->pb);
  279. for (i = 0; i < MAX_FRAME_HEADER; i++) {
  280. header[i] = avio_r8(s->pb);
  281. if (header[i] == '\n') {
  282. header[i + 1] = 0;
  283. break;
  284. }
  285. }
  286. if (s->pb->error)
  287. return s->pb->error;
  288. else if (s->pb->eof_reached)
  289. return AVERROR_EOF;
  290. else if (i == MAX_FRAME_HEADER)
  291. return AVERROR_INVALIDDATA;
  292. if (strncmp(header, Y4M_FRAME_MAGIC, strlen(Y4M_FRAME_MAGIC)))
  293. return AVERROR_INVALIDDATA;
  294. ret = av_get_packet(s->pb, pkt, s->packet_size - Y4M_FRAME_MAGIC_LEN);
  295. if (ret < 0)
  296. return ret;
  297. else if (ret != s->packet_size - Y4M_FRAME_MAGIC_LEN) {
  298. return s->pb->eof_reached ? AVERROR_EOF : AVERROR(EIO);
  299. }
  300. pkt->stream_index = 0;
  301. pkt->pts = (off - s->internal->data_offset) / s->packet_size;
  302. pkt->duration = 1;
  303. return 0;
  304. }
  305. static int yuv4_read_seek(AVFormatContext *s, int stream_index,
  306. int64_t pts, int flags)
  307. {
  308. int64_t pos;
  309. if (flags & AVSEEK_FLAG_BACKWARD)
  310. pts = FFMAX(0, pts - 1);
  311. if (pts < 0)
  312. return -1;
  313. pos = pts * s->packet_size;
  314. if (avio_seek(s->pb, pos + s->internal->data_offset, SEEK_SET) < 0)
  315. return -1;
  316. return 0;
  317. }
  318. static int yuv4_probe(const AVProbeData *pd)
  319. {
  320. /* check file header */
  321. if (strncmp(pd->buf, Y4M_MAGIC, sizeof(Y4M_MAGIC) - 1) == 0)
  322. return AVPROBE_SCORE_MAX;
  323. else
  324. return 0;
  325. }
  326. AVInputFormat ff_yuv4mpegpipe_demuxer = {
  327. .name = "yuv4mpegpipe",
  328. .long_name = NULL_IF_CONFIG_SMALL("YUV4MPEG pipe"),
  329. .read_probe = yuv4_probe,
  330. .read_header = yuv4_read_header,
  331. .read_packet = yuv4_read_packet,
  332. .read_seek = yuv4_read_seek,
  333. .extensions = "y4m",
  334. };