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.

260 lines
9.0KB

  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 "avformat.h"
  22. #include "internal.h"
  23. #include "yuv4mpeg.h"
  24. /* Header size increased to allow room for optional flags */
  25. #define MAX_YUV4_HEADER 80
  26. #define MAX_FRAME_HEADER 80
  27. static int yuv4_read_header(AVFormatContext *s)
  28. {
  29. char header[MAX_YUV4_HEADER + 10]; // Include headroom for
  30. // the longest option
  31. char *tokstart, *tokend, *header_end;
  32. int i;
  33. AVIOContext *pb = s->pb;
  34. int width = -1, height = -1, raten = 0,
  35. rated = 0, aspectn = 0, aspectd = 0;
  36. enum AVPixelFormat pix_fmt = AV_PIX_FMT_NONE, alt_pix_fmt = AV_PIX_FMT_NONE;
  37. enum AVChromaLocation chroma_sample_location = AVCHROMA_LOC_UNSPECIFIED;
  38. enum AVFieldOrder field_order = AV_FIELD_UNKNOWN;
  39. AVStream *st;
  40. for (i = 0; i < MAX_YUV4_HEADER; i++) {
  41. header[i] = avio_r8(pb);
  42. if (header[i] == '\n') {
  43. header[i + 1] = 0x20; // Add a space after last option.
  44. // Makes parsing "444" vs "444alpha" easier.
  45. header[i + 2] = 0;
  46. break;
  47. }
  48. }
  49. if (i == MAX_YUV4_HEADER)
  50. return -1;
  51. if (strncmp(header, Y4M_MAGIC, strlen(Y4M_MAGIC)))
  52. return -1;
  53. header_end = &header[i + 1]; // Include space
  54. for (tokstart = &header[strlen(Y4M_MAGIC) + 1];
  55. tokstart < header_end; tokstart++) {
  56. if (*tokstart == 0x20)
  57. continue;
  58. switch (*tokstart++) {
  59. case 'W': // Width. Required.
  60. width = strtol(tokstart, &tokend, 10);
  61. tokstart = tokend;
  62. break;
  63. case 'H': // Height. Required.
  64. height = strtol(tokstart, &tokend, 10);
  65. tokstart = tokend;
  66. break;
  67. case 'C': // Color space
  68. if (strncmp("420jpeg", tokstart, 7) == 0) {
  69. pix_fmt = AV_PIX_FMT_YUV420P;
  70. chroma_sample_location = AVCHROMA_LOC_CENTER;
  71. } else if (strncmp("420mpeg2", tokstart, 8) == 0) {
  72. pix_fmt = AV_PIX_FMT_YUV420P;
  73. chroma_sample_location = AVCHROMA_LOC_LEFT;
  74. } else if (strncmp("420paldv", tokstart, 8) == 0) {
  75. pix_fmt = AV_PIX_FMT_YUV420P;
  76. chroma_sample_location = AVCHROMA_LOC_TOPLEFT;
  77. } else if (strncmp("420", tokstart, 3) == 0) {
  78. pix_fmt = AV_PIX_FMT_YUV420P;
  79. chroma_sample_location = AVCHROMA_LOC_CENTER;
  80. } else if (strncmp("411", tokstart, 3) == 0)
  81. pix_fmt = AV_PIX_FMT_YUV411P;
  82. else if (strncmp("422", tokstart, 3) == 0)
  83. pix_fmt = AV_PIX_FMT_YUV422P;
  84. else if (strncmp("444alpha", tokstart, 8) == 0 ) {
  85. av_log(s, AV_LOG_ERROR, "Cannot handle 4:4:4:4 "
  86. "YUV4MPEG stream.\n");
  87. return -1;
  88. } else if (strncmp("444", tokstart, 3) == 0)
  89. pix_fmt = AV_PIX_FMT_YUV444P;
  90. else if (strncmp("mono", tokstart, 4) == 0) {
  91. pix_fmt = AV_PIX_FMT_GRAY8;
  92. } else {
  93. av_log(s, AV_LOG_ERROR, "YUV4MPEG stream contains an unknown "
  94. "pixel format.\n");
  95. return -1;
  96. }
  97. while (tokstart < header_end && *tokstart != 0x20)
  98. tokstart++;
  99. break;
  100. case 'I': // Interlace type
  101. switch (*tokstart++){
  102. case '?':
  103. field_order = AV_FIELD_UNKNOWN;
  104. break;
  105. case 'p':
  106. field_order = AV_FIELD_PROGRESSIVE;
  107. break;
  108. case 't':
  109. field_order = AV_FIELD_TT;
  110. break;
  111. case 'b':
  112. field_order = AV_FIELD_BB;
  113. break;
  114. case 'm':
  115. av_log(s, AV_LOG_ERROR, "YUV4MPEG stream contains mixed "
  116. "interlaced and non-interlaced frames.\n");
  117. return -1;
  118. default:
  119. av_log(s, AV_LOG_ERROR, "YUV4MPEG has invalid header.\n");
  120. return -1;
  121. }
  122. break;
  123. case 'F': // Frame rate
  124. sscanf(tokstart, "%d:%d", &raten, &rated); // 0:0 if unknown
  125. while (tokstart < header_end && *tokstart != 0x20)
  126. tokstart++;
  127. break;
  128. case 'A': // Pixel aspect
  129. sscanf(tokstart, "%d:%d", &aspectn, &aspectd); // 0:0 if unknown
  130. while (tokstart < header_end && *tokstart != 0x20)
  131. tokstart++;
  132. break;
  133. case 'X': // Vendor extensions
  134. if (strncmp("YSCSS=", tokstart, 6) == 0) {
  135. // Older nonstandard pixel format representation
  136. tokstart += 6;
  137. if (strncmp("420JPEG", tokstart, 7) == 0)
  138. alt_pix_fmt = AV_PIX_FMT_YUV420P;
  139. else if (strncmp("420MPEG2", tokstart, 8) == 0)
  140. alt_pix_fmt = AV_PIX_FMT_YUV420P;
  141. else if (strncmp("420PALDV", tokstart, 8) == 0)
  142. alt_pix_fmt = AV_PIX_FMT_YUV420P;
  143. else if (strncmp("411", tokstart, 3) == 0)
  144. alt_pix_fmt = AV_PIX_FMT_YUV411P;
  145. else if (strncmp("422", tokstart, 3) == 0)
  146. alt_pix_fmt = AV_PIX_FMT_YUV422P;
  147. else if (strncmp("444", tokstart, 3) == 0)
  148. alt_pix_fmt = AV_PIX_FMT_YUV444P;
  149. }
  150. while (tokstart < header_end && *tokstart != 0x20)
  151. tokstart++;
  152. break;
  153. }
  154. }
  155. if (width == -1 || height == -1) {
  156. av_log(s, AV_LOG_ERROR, "YUV4MPEG has invalid header.\n");
  157. return -1;
  158. }
  159. if (pix_fmt == AV_PIX_FMT_NONE) {
  160. if (alt_pix_fmt == AV_PIX_FMT_NONE)
  161. pix_fmt = AV_PIX_FMT_YUV420P;
  162. else
  163. pix_fmt = alt_pix_fmt;
  164. }
  165. if (raten <= 0 || rated <= 0) {
  166. // Frame rate unknown
  167. raten = 25;
  168. rated = 1;
  169. }
  170. if (aspectn == 0 && aspectd == 0) {
  171. // Pixel aspect unknown
  172. aspectd = 1;
  173. }
  174. st = avformat_new_stream(s, NULL);
  175. if (!st)
  176. return AVERROR(ENOMEM);
  177. st->codec->width = width;
  178. st->codec->height = height;
  179. av_reduce(&raten, &rated, raten, rated, (1UL << 31) - 1);
  180. avpriv_set_pts_info(st, 64, rated, raten);
  181. st->avg_frame_rate = av_inv_q(st->time_base);
  182. st->codec->pix_fmt = pix_fmt;
  183. st->codec->codec_type = AVMEDIA_TYPE_VIDEO;
  184. st->codec->codec_id = AV_CODEC_ID_RAWVIDEO;
  185. st->sample_aspect_ratio = (AVRational){ aspectn, aspectd };
  186. st->codec->chroma_sample_location = chroma_sample_location;
  187. st->codec->field_order = field_order;
  188. return 0;
  189. }
  190. static int yuv4_read_packet(AVFormatContext *s, AVPacket *pkt)
  191. {
  192. int i;
  193. char header[MAX_FRAME_HEADER+1];
  194. int packet_size, width, height, ret;
  195. AVStream *st = s->streams[0];
  196. for (i = 0; i < MAX_FRAME_HEADER; i++) {
  197. header[i] = avio_r8(s->pb);
  198. if (header[i] == '\n') {
  199. header[i + 1] = 0;
  200. break;
  201. }
  202. }
  203. if (s->pb->error)
  204. return s->pb->error;
  205. else if (s->pb->eof_reached)
  206. return AVERROR_EOF;
  207. else if (i == MAX_FRAME_HEADER)
  208. return AVERROR_INVALIDDATA;
  209. if (strncmp(header, Y4M_FRAME_MAGIC, strlen(Y4M_FRAME_MAGIC)))
  210. return AVERROR_INVALIDDATA;
  211. width = st->codec->width;
  212. height = st->codec->height;
  213. packet_size = avpicture_get_size(st->codec->pix_fmt, width, height);
  214. if (packet_size < 0)
  215. return packet_size;
  216. ret = av_get_packet(s->pb, pkt, packet_size);
  217. if (ret < 0)
  218. return ret;
  219. else if (ret != packet_size)
  220. return s->pb->eof_reached ? AVERROR_EOF : AVERROR(EIO);
  221. pkt->stream_index = 0;
  222. return 0;
  223. }
  224. static int yuv4_probe(AVProbeData *pd)
  225. {
  226. /* check file header */
  227. if (strncmp(pd->buf, Y4M_MAGIC, sizeof(Y4M_MAGIC) - 1) == 0)
  228. return AVPROBE_SCORE_MAX;
  229. else
  230. return 0;
  231. }
  232. AVInputFormat ff_yuv4mpegpipe_demuxer = {
  233. .name = "yuv4mpegpipe",
  234. .long_name = NULL_IF_CONFIG_SMALL("YUV4MPEG pipe"),
  235. .read_probe = yuv4_probe,
  236. .read_header = yuv4_read_header,
  237. .read_packet = yuv4_read_packet,
  238. .extensions = "y4m",
  239. };