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.

263 lines
9.1KB

  1. /*
  2. * YUV4MPEG demuxer
  3. * Copyright (c) 2001, 2002, 2003 Fabrice Bellard
  4. *
  5. * This file is part of Libav.
  6. *
  7. * Libav 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. * Libav 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 Libav; 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("420", tokstart, 3) == 0) {
  79. pix_fmt = AV_PIX_FMT_YUV420P;
  80. chroma_sample_location = AVCHROMA_LOC_CENTER;
  81. } else if (strncmp("411", tokstart, 3) == 0)
  82. pix_fmt = AV_PIX_FMT_YUV411P;
  83. else if (strncmp("422", tokstart, 3) == 0)
  84. pix_fmt = AV_PIX_FMT_YUV422P;
  85. else if (strncmp("444alpha", tokstart, 8) == 0 ) {
  86. av_log(s, AV_LOG_ERROR, "Cannot handle 4:4:4:4 "
  87. "YUV4MPEG stream.\n");
  88. return -1;
  89. } else if (strncmp("444", tokstart, 3) == 0)
  90. pix_fmt = AV_PIX_FMT_YUV444P;
  91. else if (strncmp("mono", tokstart, 4) == 0) {
  92. pix_fmt = AV_PIX_FMT_GRAY8;
  93. } else {
  94. av_log(s, AV_LOG_ERROR, "YUV4MPEG stream contains an unknown "
  95. "pixel format.\n");
  96. return -1;
  97. }
  98. while (tokstart < header_end && *tokstart != 0x20)
  99. tokstart++;
  100. break;
  101. case 'I': // Interlace type
  102. switch (*tokstart++){
  103. case '?':
  104. field_order = AV_FIELD_UNKNOWN;
  105. break;
  106. case 'p':
  107. field_order = AV_FIELD_PROGRESSIVE;
  108. break;
  109. case 't':
  110. field_order = AV_FIELD_TT;
  111. break;
  112. case 'b':
  113. field_order = AV_FIELD_BB;
  114. break;
  115. case 'm':
  116. av_log(s, AV_LOG_ERROR, "YUV4MPEG stream contains mixed "
  117. "interlaced and non-interlaced frames.\n");
  118. return -1;
  119. default:
  120. av_log(s, AV_LOG_ERROR, "YUV4MPEG has invalid header.\n");
  121. return -1;
  122. }
  123. break;
  124. case 'F': // Frame rate
  125. sscanf(tokstart, "%d:%d", &raten, &rated); // 0:0 if unknown
  126. while (tokstart < header_end && *tokstart != 0x20)
  127. tokstart++;
  128. break;
  129. case 'A': // Pixel aspect
  130. sscanf(tokstart, "%d:%d", &aspectn, &aspectd); // 0:0 if unknown
  131. while (tokstart < header_end && *tokstart != 0x20)
  132. tokstart++;
  133. break;
  134. case 'X': // Vendor extensions
  135. if (strncmp("YSCSS=", tokstart, 6) == 0) {
  136. // Older nonstandard pixel format representation
  137. tokstart += 6;
  138. if (strncmp("420JPEG", tokstart, 7) == 0)
  139. alt_pix_fmt = AV_PIX_FMT_YUV420P;
  140. else if (strncmp("420MPEG2", tokstart, 8) == 0)
  141. alt_pix_fmt = AV_PIX_FMT_YUV420P;
  142. else if (strncmp("420PALDV", tokstart, 8) == 0)
  143. alt_pix_fmt = AV_PIX_FMT_YUV420P;
  144. else if (strncmp("411", tokstart, 3) == 0)
  145. alt_pix_fmt = AV_PIX_FMT_YUV411P;
  146. else if (strncmp("422", tokstart, 3) == 0)
  147. alt_pix_fmt = AV_PIX_FMT_YUV422P;
  148. else if (strncmp("444", tokstart, 3) == 0)
  149. alt_pix_fmt = AV_PIX_FMT_YUV444P;
  150. }
  151. while (tokstart < header_end && *tokstart != 0x20)
  152. tokstart++;
  153. break;
  154. }
  155. }
  156. if (width == -1 || height == -1) {
  157. av_log(s, AV_LOG_ERROR, "YUV4MPEG has invalid header.\n");
  158. return -1;
  159. }
  160. if (pix_fmt == AV_PIX_FMT_NONE) {
  161. if (alt_pix_fmt == AV_PIX_FMT_NONE)
  162. pix_fmt = AV_PIX_FMT_YUV420P;
  163. else
  164. pix_fmt = alt_pix_fmt;
  165. }
  166. if (raten <= 0 || rated <= 0) {
  167. // Frame rate unknown
  168. raten = 25;
  169. rated = 1;
  170. }
  171. if (aspectn == 0 && aspectd == 0) {
  172. // Pixel aspect unknown
  173. aspectd = 1;
  174. }
  175. st = avformat_new_stream(s, NULL);
  176. if (!st)
  177. return AVERROR(ENOMEM);
  178. st->codecpar->width = width;
  179. st->codecpar->height = height;
  180. av_reduce(&raten, &rated, raten, rated, (1UL << 31) - 1);
  181. avpriv_set_pts_info(st, 64, rated, raten);
  182. st->avg_frame_rate = av_inv_q(st->time_base);
  183. st->codecpar->format = pix_fmt;
  184. st->codecpar->codec_type = AVMEDIA_TYPE_VIDEO;
  185. st->codecpar->codec_id = AV_CODEC_ID_RAWVIDEO;
  186. st->sample_aspect_ratio = (AVRational){ aspectn, aspectd };
  187. st->codecpar->chroma_location = chroma_sample_location;
  188. st->codecpar->field_order = field_order;
  189. return 0;
  190. }
  191. static int yuv4_read_packet(AVFormatContext *s, AVPacket *pkt)
  192. {
  193. int i;
  194. char header[MAX_FRAME_HEADER+1];
  195. int packet_size, width, height, ret;
  196. AVStream *st = s->streams[0];
  197. for (i = 0; i < MAX_FRAME_HEADER; i++) {
  198. header[i] = avio_r8(s->pb);
  199. if (header[i] == '\n') {
  200. header[i + 1] = 0;
  201. break;
  202. }
  203. }
  204. if (s->pb->error)
  205. return s->pb->error;
  206. else if (s->pb->eof_reached)
  207. return AVERROR_EOF;
  208. else if (i == MAX_FRAME_HEADER)
  209. return AVERROR_INVALIDDATA;
  210. if (strncmp(header, Y4M_FRAME_MAGIC, strlen(Y4M_FRAME_MAGIC)))
  211. return AVERROR_INVALIDDATA;
  212. width = st->codecpar->width;
  213. height = st->codecpar->height;
  214. packet_size = av_image_get_buffer_size(st->codecpar->format,
  215. width, height, 1);
  216. if (packet_size < 0)
  217. return packet_size;
  218. ret = av_get_packet(s->pb, pkt, packet_size);
  219. if (ret < 0)
  220. return ret;
  221. else if (ret != packet_size)
  222. return s->pb->eof_reached ? AVERROR_EOF : AVERROR(EIO);
  223. pkt->stream_index = 0;
  224. return 0;
  225. }
  226. static int yuv4_probe(AVProbeData *pd)
  227. {
  228. /* check file header */
  229. if (strncmp(pd->buf, Y4M_MAGIC, sizeof(Y4M_MAGIC) - 1) == 0)
  230. return AVPROBE_SCORE_MAX;
  231. else
  232. return 0;
  233. }
  234. AVInputFormat ff_yuv4mpegpipe_demuxer = {
  235. .name = "yuv4mpegpipe",
  236. .long_name = NULL_IF_CONFIG_SMALL("YUV4MPEG pipe"),
  237. .read_probe = yuv4_probe,
  238. .read_header = yuv4_read_header,
  239. .read_packet = yuv4_read_packet,
  240. .extensions = "y4m",
  241. };