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.

274 lines
9.4KB

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