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.

333 lines
13KB

  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 80
  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. AVStream *st;
  41. for (i = 0; i < MAX_YUV4_HEADER; i++) {
  42. header[i] = avio_r8(pb);
  43. if (header[i] == '\n') {
  44. header[i + 1] = 0x20; // Add a space after last option.
  45. // Makes parsing "444" vs "444alpha" easier.
  46. header[i + 2] = 0;
  47. break;
  48. }
  49. }
  50. if (i == MAX_YUV4_HEADER)
  51. return -1;
  52. if (strncmp(header, Y4M_MAGIC, strlen(Y4M_MAGIC)))
  53. return -1;
  54. header_end = &header[i + 1]; // Include space
  55. for (tokstart = &header[strlen(Y4M_MAGIC) + 1];
  56. tokstart < header_end; tokstart++) {
  57. if (*tokstart == 0x20)
  58. continue;
  59. switch (*tokstart++) {
  60. case 'W': // Width. Required.
  61. width = strtol(tokstart, &tokend, 10);
  62. tokstart = tokend;
  63. break;
  64. case 'H': // Height. Required.
  65. height = strtol(tokstart, &tokend, 10);
  66. tokstart = tokend;
  67. break;
  68. case 'C': // Color space
  69. if (strncmp("420jpeg", tokstart, 7) == 0) {
  70. pix_fmt = AV_PIX_FMT_YUV420P;
  71. chroma_sample_location = AVCHROMA_LOC_CENTER;
  72. } else if (strncmp("420mpeg2", tokstart, 8) == 0) {
  73. pix_fmt = AV_PIX_FMT_YUV420P;
  74. chroma_sample_location = AVCHROMA_LOC_LEFT;
  75. } else if (strncmp("420paldv", tokstart, 8) == 0) {
  76. pix_fmt = AV_PIX_FMT_YUV420P;
  77. chroma_sample_location = AVCHROMA_LOC_TOPLEFT;
  78. } else if (strncmp("420p16", tokstart, 6) == 0) {
  79. pix_fmt = AV_PIX_FMT_YUV420P16;
  80. } else if (strncmp("422p16", tokstart, 6) == 0) {
  81. pix_fmt = AV_PIX_FMT_YUV422P16;
  82. } else if (strncmp("444p16", tokstart, 6) == 0) {
  83. pix_fmt = AV_PIX_FMT_YUV444P16;
  84. } else if (strncmp("420p14", tokstart, 6) == 0) {
  85. pix_fmt = AV_PIX_FMT_YUV420P14;
  86. } else if (strncmp("422p14", tokstart, 6) == 0) {
  87. pix_fmt = AV_PIX_FMT_YUV422P14;
  88. } else if (strncmp("444p14", tokstart, 6) == 0) {
  89. pix_fmt = AV_PIX_FMT_YUV444P14;
  90. } else if (strncmp("420p12", tokstart, 6) == 0) {
  91. pix_fmt = AV_PIX_FMT_YUV420P12;
  92. } else if (strncmp("422p12", tokstart, 6) == 0) {
  93. pix_fmt = AV_PIX_FMT_YUV422P12;
  94. } else if (strncmp("444p12", tokstart, 6) == 0) {
  95. pix_fmt = AV_PIX_FMT_YUV444P12;
  96. } else if (strncmp("420p10", tokstart, 6) == 0) {
  97. pix_fmt = AV_PIX_FMT_YUV420P10;
  98. } else if (strncmp("422p10", tokstart, 6) == 0) {
  99. pix_fmt = AV_PIX_FMT_YUV422P10;
  100. } else if (strncmp("444p10", tokstart, 6) == 0) {
  101. pix_fmt = AV_PIX_FMT_YUV444P10;
  102. } else if (strncmp("420p9", tokstart, 5) == 0) {
  103. pix_fmt = AV_PIX_FMT_YUV420P9;
  104. } else if (strncmp("422p9", tokstart, 5) == 0) {
  105. pix_fmt = AV_PIX_FMT_YUV422P9;
  106. } else if (strncmp("444p9", tokstart, 5) == 0) {
  107. pix_fmt = AV_PIX_FMT_YUV444P9;
  108. } else if (strncmp("420", tokstart, 3) == 0) {
  109. pix_fmt = AV_PIX_FMT_YUV420P;
  110. chroma_sample_location = AVCHROMA_LOC_CENTER;
  111. } else if (strncmp("411", tokstart, 3) == 0) {
  112. pix_fmt = AV_PIX_FMT_YUV411P;
  113. } else if (strncmp("422", tokstart, 3) == 0) {
  114. pix_fmt = AV_PIX_FMT_YUV422P;
  115. } else if (strncmp("444alpha", tokstart, 8) == 0 ) {
  116. av_log(s, AV_LOG_ERROR, "Cannot handle 4:4:4:4 "
  117. "YUV4MPEG stream.\n");
  118. return -1;
  119. } else if (strncmp("444", tokstart, 3) == 0) {
  120. pix_fmt = AV_PIX_FMT_YUV444P;
  121. } else if (strncmp("mono16", tokstart, 6) == 0) {
  122. pix_fmt = AV_PIX_FMT_GRAY16;
  123. } else if (strncmp("mono", tokstart, 4) == 0) {
  124. pix_fmt = AV_PIX_FMT_GRAY8;
  125. } else {
  126. av_log(s, AV_LOG_ERROR, "YUV4MPEG stream contains an unknown "
  127. "pixel format.\n");
  128. return -1;
  129. }
  130. while (tokstart < header_end && *tokstart != 0x20)
  131. tokstart++;
  132. break;
  133. case 'I': // Interlace type
  134. switch (*tokstart++){
  135. case '?':
  136. field_order = AV_FIELD_UNKNOWN;
  137. break;
  138. case 'p':
  139. field_order = AV_FIELD_PROGRESSIVE;
  140. break;
  141. case 't':
  142. field_order = AV_FIELD_TT;
  143. break;
  144. case 'b':
  145. field_order = AV_FIELD_BB;
  146. break;
  147. case 'm':
  148. av_log(s, AV_LOG_ERROR, "YUV4MPEG stream contains mixed "
  149. "interlaced and non-interlaced frames.\n");
  150. default:
  151. av_log(s, AV_LOG_ERROR, "YUV4MPEG has invalid header.\n");
  152. return AVERROR(EINVAL);
  153. }
  154. break;
  155. case 'F': // Frame rate
  156. sscanf(tokstart, "%d:%d", &raten, &rated); // 0:0 if unknown
  157. while (tokstart < header_end && *tokstart != 0x20)
  158. tokstart++;
  159. break;
  160. case 'A': // Pixel aspect
  161. sscanf(tokstart, "%d:%d", &aspectn, &aspectd); // 0:0 if unknown
  162. while (tokstart < header_end && *tokstart != 0x20)
  163. tokstart++;
  164. break;
  165. case 'X': // Vendor extensions
  166. if (strncmp("YSCSS=", tokstart, 6) == 0) {
  167. // Older nonstandard pixel format representation
  168. tokstart += 6;
  169. if (strncmp("420JPEG", tokstart, 7) == 0)
  170. alt_pix_fmt = AV_PIX_FMT_YUV420P;
  171. else if (strncmp("420MPEG2", tokstart, 8) == 0)
  172. alt_pix_fmt = AV_PIX_FMT_YUV420P;
  173. else if (strncmp("420PALDV", tokstart, 8) == 0)
  174. alt_pix_fmt = AV_PIX_FMT_YUV420P;
  175. else if (strncmp("420P9", tokstart, 5) == 0)
  176. alt_pix_fmt = AV_PIX_FMT_YUV420P9;
  177. else if (strncmp("422P9", tokstart, 5) == 0)
  178. alt_pix_fmt = AV_PIX_FMT_YUV422P9;
  179. else if (strncmp("444P9", tokstart, 5) == 0)
  180. alt_pix_fmt = AV_PIX_FMT_YUV444P9;
  181. else if (strncmp("420P10", tokstart, 6) == 0)
  182. alt_pix_fmt = AV_PIX_FMT_YUV420P10;
  183. else if (strncmp("422P10", tokstart, 6) == 0)
  184. alt_pix_fmt = AV_PIX_FMT_YUV422P10;
  185. else if (strncmp("444P10", tokstart, 6) == 0)
  186. alt_pix_fmt = AV_PIX_FMT_YUV444P10;
  187. else if (strncmp("420P12", tokstart, 6) == 0)
  188. alt_pix_fmt = AV_PIX_FMT_YUV420P12;
  189. else if (strncmp("422P12", tokstart, 6) == 0)
  190. alt_pix_fmt = AV_PIX_FMT_YUV422P12;
  191. else if (strncmp("444P12", tokstart, 6) == 0)
  192. alt_pix_fmt = AV_PIX_FMT_YUV444P12;
  193. else if (strncmp("420P14", tokstart, 6) == 0)
  194. alt_pix_fmt = AV_PIX_FMT_YUV420P14;
  195. else if (strncmp("422P14", tokstart, 6) == 0)
  196. alt_pix_fmt = AV_PIX_FMT_YUV422P14;
  197. else if (strncmp("444P14", tokstart, 6) == 0)
  198. alt_pix_fmt = AV_PIX_FMT_YUV444P14;
  199. else if (strncmp("420P16", tokstart, 6) == 0)
  200. alt_pix_fmt = AV_PIX_FMT_YUV420P16;
  201. else if (strncmp("422P16", tokstart, 6) == 0)
  202. alt_pix_fmt = AV_PIX_FMT_YUV422P16;
  203. else if (strncmp("444P16", tokstart, 6) == 0)
  204. alt_pix_fmt = AV_PIX_FMT_YUV444P16;
  205. else if (strncmp("411", tokstart, 3) == 0)
  206. alt_pix_fmt = AV_PIX_FMT_YUV411P;
  207. else if (strncmp("422", tokstart, 3) == 0)
  208. alt_pix_fmt = AV_PIX_FMT_YUV422P;
  209. else if (strncmp("444", tokstart, 3) == 0)
  210. alt_pix_fmt = AV_PIX_FMT_YUV444P;
  211. }
  212. while (tokstart < header_end && *tokstart != 0x20)
  213. tokstart++;
  214. break;
  215. }
  216. }
  217. if (width == -1 || height == -1) {
  218. av_log(s, AV_LOG_ERROR, "YUV4MPEG has invalid header.\n");
  219. return -1;
  220. }
  221. if (pix_fmt == AV_PIX_FMT_NONE) {
  222. if (alt_pix_fmt == AV_PIX_FMT_NONE)
  223. pix_fmt = AV_PIX_FMT_YUV420P;
  224. else
  225. pix_fmt = alt_pix_fmt;
  226. }
  227. if (raten <= 0 || rated <= 0) {
  228. // Frame rate unknown
  229. raten = 25;
  230. rated = 1;
  231. }
  232. if (aspectn == 0 && aspectd == 0) {
  233. // Pixel aspect unknown
  234. aspectd = 1;
  235. }
  236. st = avformat_new_stream(s, NULL);
  237. if (!st)
  238. return AVERROR(ENOMEM);
  239. st->codec->width = width;
  240. st->codec->height = height;
  241. av_reduce(&raten, &rated, raten, rated, (1UL << 31) - 1);
  242. avpriv_set_pts_info(st, 64, rated, raten);
  243. st->avg_frame_rate = av_inv_q(st->time_base);
  244. st->codec->pix_fmt = pix_fmt;
  245. st->codec->codec_type = AVMEDIA_TYPE_VIDEO;
  246. st->codec->codec_id = AV_CODEC_ID_RAWVIDEO;
  247. st->sample_aspect_ratio = (AVRational){ aspectn, aspectd };
  248. st->codec->chroma_sample_location = chroma_sample_location;
  249. st->codec->field_order = field_order;
  250. s->packet_size = av_image_get_buffer_size(st->codec->pix_fmt, width, height, 1) + Y4M_FRAME_MAGIC_LEN;
  251. if ((int) s->packet_size < 0)
  252. return s->packet_size;
  253. s->internal->data_offset = avio_tell(pb);
  254. st->duration = (avio_size(pb) - avio_tell(pb)) / s->packet_size;
  255. return 0;
  256. }
  257. static int yuv4_read_packet(AVFormatContext *s, AVPacket *pkt)
  258. {
  259. int i;
  260. char header[MAX_FRAME_HEADER+1];
  261. int ret;
  262. int64_t off = avio_tell(s->pb);
  263. for (i = 0; i < MAX_FRAME_HEADER; i++) {
  264. header[i] = avio_r8(s->pb);
  265. if (header[i] == '\n') {
  266. header[i + 1] = 0;
  267. break;
  268. }
  269. }
  270. if (s->pb->error)
  271. return s->pb->error;
  272. else if (s->pb->eof_reached)
  273. return AVERROR_EOF;
  274. else if (i == MAX_FRAME_HEADER)
  275. return AVERROR_INVALIDDATA;
  276. if (strncmp(header, Y4M_FRAME_MAGIC, strlen(Y4M_FRAME_MAGIC)))
  277. return AVERROR_INVALIDDATA;
  278. ret = av_get_packet(s->pb, pkt, s->packet_size - Y4M_FRAME_MAGIC_LEN);
  279. if (ret < 0)
  280. return ret;
  281. else if (ret != s->packet_size - Y4M_FRAME_MAGIC_LEN)
  282. return s->pb->eof_reached ? AVERROR_EOF : AVERROR(EIO);
  283. pkt->stream_index = 0;
  284. pkt->pts = (off - s->internal->data_offset) / s->packet_size;
  285. pkt->duration = 1;
  286. return 0;
  287. }
  288. static int yuv4_read_seek(AVFormatContext *s, int stream_index,
  289. int64_t pts, int flags)
  290. {
  291. if (avio_seek(s->pb, pts * s->packet_size + s->internal->data_offset, SEEK_SET) < 0)
  292. return -1;
  293. return 0;
  294. }
  295. static int yuv4_probe(AVProbeData *pd)
  296. {
  297. /* check file header */
  298. if (strncmp(pd->buf, Y4M_MAGIC, sizeof(Y4M_MAGIC) - 1) == 0)
  299. return AVPROBE_SCORE_MAX;
  300. else
  301. return 0;
  302. }
  303. AVInputFormat ff_yuv4mpegpipe_demuxer = {
  304. .name = "yuv4mpegpipe",
  305. .long_name = NULL_IF_CONFIG_SMALL("YUV4MPEG pipe"),
  306. .read_probe = yuv4_probe,
  307. .read_header = yuv4_read_header,
  308. .read_packet = yuv4_read_packet,
  309. .read_seek = yuv4_read_seek,
  310. .extensions = "y4m",
  311. };