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.

440 lines
14KB

  1. /*
  2. * YUV4MPEG format
  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/pixdesc.h"
  22. #include "avformat.h"
  23. #include "internal.h"
  24. #define Y4M_MAGIC "YUV4MPEG2"
  25. #define Y4M_FRAME_MAGIC "FRAME"
  26. #define Y4M_LINE_MAX 256
  27. struct frame_attributes {
  28. int interlaced_frame;
  29. int top_field_first;
  30. };
  31. #if CONFIG_YUV4MPEGPIPE_MUXER
  32. static int yuv4_generate_header(AVFormatContext *s, char* buf)
  33. {
  34. AVStream *st;
  35. int width, height;
  36. int raten, rated, aspectn, aspectd, n;
  37. char inter;
  38. const char *colorspace = "";
  39. st = s->streams[0];
  40. width = st->codec->width;
  41. height = st->codec->height;
  42. av_reduce(&raten, &rated, st->codec->time_base.den,
  43. st->codec->time_base.num, (1UL << 31) - 1);
  44. aspectn = st->sample_aspect_ratio.num;
  45. aspectd = st->sample_aspect_ratio.den;
  46. if (aspectn == 0 && aspectd == 1)
  47. aspectd = 0; // 0:0 means unknown
  48. inter = 'p'; /* progressive is the default */
  49. if (st->codec->coded_frame && st->codec->coded_frame->interlaced_frame)
  50. inter = st->codec->coded_frame->top_field_first ? 't' : 'b';
  51. switch (st->codec->pix_fmt) {
  52. case AV_PIX_FMT_GRAY8:
  53. colorspace = " Cmono";
  54. break;
  55. case AV_PIX_FMT_YUV411P:
  56. colorspace = " C411 XYSCSS=411";
  57. break;
  58. case AV_PIX_FMT_YUV420P:
  59. switch (st->codec->chroma_sample_location) {
  60. case AVCHROMA_LOC_TOPLEFT: colorspace = " C420paldv XYSCSS=420PALDV"; break;
  61. case AVCHROMA_LOC_LEFT: colorspace = " C420mpeg2 XYSCSS=420MPEG2"; break;
  62. default: colorspace = " C420jpeg XYSCSS=420JPEG"; break;
  63. }
  64. break;
  65. case AV_PIX_FMT_YUV422P:
  66. colorspace = " C422 XYSCSS=422";
  67. break;
  68. case AV_PIX_FMT_YUV444P:
  69. colorspace = " C444 XYSCSS=444";
  70. break;
  71. }
  72. /* construct stream header, if this is the first frame */
  73. n = snprintf(buf, Y4M_LINE_MAX, "%s W%d H%d F%d:%d I%c A%d:%d%s\n",
  74. Y4M_MAGIC, width, height, raten, rated, inter,
  75. aspectn, aspectd, colorspace);
  76. return n;
  77. }
  78. static int yuv4_write_packet(AVFormatContext *s, AVPacket *pkt)
  79. {
  80. AVStream *st = s->streams[pkt->stream_index];
  81. AVIOContext *pb = s->pb;
  82. AVPicture *picture;
  83. int* first_pkt = s->priv_data;
  84. int width, height, h_chroma_shift, v_chroma_shift;
  85. int i;
  86. char buf2[Y4M_LINE_MAX + 1];
  87. char buf1[20];
  88. uint8_t *ptr, *ptr1, *ptr2;
  89. picture = (AVPicture *)pkt->data;
  90. /* for the first packet we have to output the header as well */
  91. if (*first_pkt) {
  92. *first_pkt = 0;
  93. if (yuv4_generate_header(s, buf2) < 0) {
  94. av_log(s, AV_LOG_ERROR,
  95. "Error. YUV4MPEG stream header write failed.\n");
  96. return AVERROR(EIO);
  97. } else {
  98. avio_write(pb, buf2, strlen(buf2));
  99. }
  100. }
  101. /* construct frame header */
  102. snprintf(buf1, sizeof(buf1), "%s\n", Y4M_FRAME_MAGIC);
  103. avio_write(pb, buf1, strlen(buf1));
  104. width = st->codec->width;
  105. height = st->codec->height;
  106. ptr = picture->data[0];
  107. for (i = 0; i < height; i++) {
  108. avio_write(pb, ptr, width);
  109. ptr += picture->linesize[0];
  110. }
  111. if (st->codec->pix_fmt != AV_PIX_FMT_GRAY8) {
  112. // Adjust for smaller Cb and Cr planes
  113. av_pix_fmt_get_chroma_sub_sample(st->codec->pix_fmt, &h_chroma_shift,
  114. &v_chroma_shift);
  115. width >>= h_chroma_shift;
  116. height >>= v_chroma_shift;
  117. ptr1 = picture->data[1];
  118. ptr2 = picture->data[2];
  119. for (i = 0; i < height; i++) { /* Cb */
  120. avio_write(pb, ptr1, width);
  121. ptr1 += picture->linesize[1];
  122. }
  123. for (i = 0; i < height; i++) { /* Cr */
  124. avio_write(pb, ptr2, width);
  125. ptr2 += picture->linesize[2];
  126. }
  127. }
  128. avio_flush(pb);
  129. return 0;
  130. }
  131. static int yuv4_write_header(AVFormatContext *s)
  132. {
  133. int *first_pkt = s->priv_data;
  134. if (s->nb_streams != 1)
  135. return AVERROR(EIO);
  136. if (s->streams[0]->codec->codec_id != AV_CODEC_ID_RAWVIDEO) {
  137. av_log(s, AV_LOG_ERROR, "ERROR: Only rawvideo supported.\n");
  138. return AVERROR_INVALIDDATA;
  139. }
  140. if (s->streams[0]->codec->pix_fmt == AV_PIX_FMT_YUV411P) {
  141. av_log(s, AV_LOG_ERROR, "Warning: generating rarely used 4:1:1 YUV "
  142. "stream, some mjpegtools might not work.\n");
  143. } else if ((s->streams[0]->codec->pix_fmt != AV_PIX_FMT_YUV420P) &&
  144. (s->streams[0]->codec->pix_fmt != AV_PIX_FMT_YUV422P) &&
  145. (s->streams[0]->codec->pix_fmt != AV_PIX_FMT_GRAY8) &&
  146. (s->streams[0]->codec->pix_fmt != AV_PIX_FMT_YUV444P)) {
  147. av_log(s, AV_LOG_ERROR, "ERROR: yuv4mpeg only handles yuv444p, "
  148. "yuv422p, yuv420p, yuv411p and gray pixel formats. "
  149. "Use -pix_fmt to select one.\n");
  150. return AVERROR(EIO);
  151. }
  152. *first_pkt = 1;
  153. return 0;
  154. }
  155. AVOutputFormat ff_yuv4mpegpipe_muxer = {
  156. .name = "yuv4mpegpipe",
  157. .long_name = NULL_IF_CONFIG_SMALL("YUV4MPEG pipe"),
  158. .mime_type = "",
  159. .extensions = "y4m",
  160. .priv_data_size = sizeof(int),
  161. .audio_codec = AV_CODEC_ID_NONE,
  162. .video_codec = AV_CODEC_ID_RAWVIDEO,
  163. .write_header = yuv4_write_header,
  164. .write_packet = yuv4_write_packet,
  165. .flags = AVFMT_RAWPICTURE,
  166. };
  167. #endif
  168. /* Header size increased to allow room for optional flags */
  169. #define MAX_YUV4_HEADER 80
  170. #define MAX_FRAME_HEADER 80
  171. static int yuv4_read_header(AVFormatContext *s)
  172. {
  173. char header[MAX_YUV4_HEADER + 10]; // Include headroom for
  174. // the longest option
  175. char *tokstart, *tokend, *header_end;
  176. int i;
  177. AVIOContext *pb = s->pb;
  178. int width = -1, height = -1, raten = 0,
  179. rated = 0, aspectn = 0, aspectd = 0;
  180. enum AVPixelFormat pix_fmt = AV_PIX_FMT_NONE, alt_pix_fmt = AV_PIX_FMT_NONE;
  181. enum AVChromaLocation chroma_sample_location = AVCHROMA_LOC_UNSPECIFIED;
  182. AVStream *st;
  183. struct frame_attributes *s1 = s->priv_data;
  184. for (i = 0; i < MAX_YUV4_HEADER; i++) {
  185. header[i] = avio_r8(pb);
  186. if (header[i] == '\n') {
  187. header[i + 1] = 0x20; // Add a space after last option.
  188. // Makes parsing "444" vs "444alpha" easier.
  189. header[i + 2] = 0;
  190. break;
  191. }
  192. }
  193. if (i == MAX_YUV4_HEADER)
  194. return -1;
  195. if (strncmp(header, Y4M_MAGIC, strlen(Y4M_MAGIC)))
  196. return -1;
  197. s1->interlaced_frame = 0;
  198. s1->top_field_first = 0;
  199. header_end = &header[i + 1]; // Include space
  200. for (tokstart = &header[strlen(Y4M_MAGIC) + 1];
  201. tokstart < header_end; tokstart++) {
  202. if (*tokstart == 0x20)
  203. continue;
  204. switch (*tokstart++) {
  205. case 'W': // Width. Required.
  206. width = strtol(tokstart, &tokend, 10);
  207. tokstart = tokend;
  208. break;
  209. case 'H': // Height. Required.
  210. height = strtol(tokstart, &tokend, 10);
  211. tokstart = tokend;
  212. break;
  213. case 'C': // Color space
  214. if (strncmp("420jpeg", tokstart, 7) == 0) {
  215. pix_fmt = AV_PIX_FMT_YUV420P;
  216. chroma_sample_location = AVCHROMA_LOC_CENTER;
  217. } else if (strncmp("420mpeg2", tokstart, 8) == 0) {
  218. pix_fmt = AV_PIX_FMT_YUV420P;
  219. chroma_sample_location = AVCHROMA_LOC_LEFT;
  220. } else if (strncmp("420paldv", tokstart, 8) == 0) {
  221. pix_fmt = AV_PIX_FMT_YUV420P;
  222. chroma_sample_location = AVCHROMA_LOC_TOPLEFT;
  223. } else if (strncmp("420", tokstart, 3) == 0) {
  224. pix_fmt = AV_PIX_FMT_YUV420P;
  225. chroma_sample_location = AVCHROMA_LOC_CENTER;
  226. } else if (strncmp("411", tokstart, 3) == 0)
  227. pix_fmt = AV_PIX_FMT_YUV411P;
  228. else if (strncmp("422", tokstart, 3) == 0)
  229. pix_fmt = AV_PIX_FMT_YUV422P;
  230. else if (strncmp("444alpha", tokstart, 8) == 0 ) {
  231. av_log(s, AV_LOG_ERROR, "Cannot handle 4:4:4:4 "
  232. "YUV4MPEG stream.\n");
  233. return -1;
  234. } else if (strncmp("444", tokstart, 3) == 0)
  235. pix_fmt = AV_PIX_FMT_YUV444P;
  236. else if (strncmp("mono", tokstart, 4) == 0) {
  237. pix_fmt = AV_PIX_FMT_GRAY8;
  238. } else {
  239. av_log(s, AV_LOG_ERROR, "YUV4MPEG stream contains an unknown "
  240. "pixel format.\n");
  241. return -1;
  242. }
  243. while (tokstart < header_end && *tokstart != 0x20)
  244. tokstart++;
  245. break;
  246. case 'I': // Interlace type
  247. switch (*tokstart++){
  248. case '?':
  249. break;
  250. case 'p':
  251. s1->interlaced_frame = 0;
  252. break;
  253. case 't':
  254. s1->interlaced_frame = 1;
  255. s1->top_field_first = 1;
  256. break;
  257. case 'b':
  258. s1->interlaced_frame = 1;
  259. s1->top_field_first = 0;
  260. break;
  261. case 'm':
  262. av_log(s, AV_LOG_ERROR, "YUV4MPEG stream contains mixed "
  263. "interlaced and non-interlaced frames.\n");
  264. return -1;
  265. default:
  266. av_log(s, AV_LOG_ERROR, "YUV4MPEG has invalid header.\n");
  267. return -1;
  268. }
  269. break;
  270. case 'F': // Frame rate
  271. sscanf(tokstart, "%d:%d", &raten, &rated); // 0:0 if unknown
  272. while (tokstart < header_end && *tokstart != 0x20)
  273. tokstart++;
  274. break;
  275. case 'A': // Pixel aspect
  276. sscanf(tokstart, "%d:%d", &aspectn, &aspectd); // 0:0 if unknown
  277. while (tokstart < header_end && *tokstart != 0x20)
  278. tokstart++;
  279. break;
  280. case 'X': // Vendor extensions
  281. if (strncmp("YSCSS=", tokstart, 6) == 0) {
  282. // Older nonstandard pixel format representation
  283. tokstart += 6;
  284. if (strncmp("420JPEG", tokstart, 7) == 0)
  285. alt_pix_fmt = AV_PIX_FMT_YUV420P;
  286. else if (strncmp("420MPEG2", tokstart, 8) == 0)
  287. alt_pix_fmt = AV_PIX_FMT_YUV420P;
  288. else if (strncmp("420PALDV", tokstart, 8) == 0)
  289. alt_pix_fmt = AV_PIX_FMT_YUV420P;
  290. else if (strncmp("411", tokstart, 3) == 0)
  291. alt_pix_fmt = AV_PIX_FMT_YUV411P;
  292. else if (strncmp("422", tokstart, 3) == 0)
  293. alt_pix_fmt = AV_PIX_FMT_YUV422P;
  294. else if (strncmp("444", tokstart, 3) == 0)
  295. alt_pix_fmt = AV_PIX_FMT_YUV444P;
  296. }
  297. while (tokstart < header_end && *tokstart != 0x20)
  298. tokstart++;
  299. break;
  300. }
  301. }
  302. if (width == -1 || height == -1) {
  303. av_log(s, AV_LOG_ERROR, "YUV4MPEG has invalid header.\n");
  304. return -1;
  305. }
  306. if (pix_fmt == AV_PIX_FMT_NONE) {
  307. if (alt_pix_fmt == AV_PIX_FMT_NONE)
  308. pix_fmt = AV_PIX_FMT_YUV420P;
  309. else
  310. pix_fmt = alt_pix_fmt;
  311. }
  312. if (raten <= 0 || rated <= 0) {
  313. // Frame rate unknown
  314. raten = 25;
  315. rated = 1;
  316. }
  317. if (aspectn == 0 && aspectd == 0) {
  318. // Pixel aspect unknown
  319. aspectd = 1;
  320. }
  321. st = avformat_new_stream(s, NULL);
  322. if (!st)
  323. return AVERROR(ENOMEM);
  324. st->codec->width = width;
  325. st->codec->height = height;
  326. av_reduce(&raten, &rated, raten, rated, (1UL << 31) - 1);
  327. avpriv_set_pts_info(st, 64, rated, raten);
  328. st->codec->pix_fmt = pix_fmt;
  329. st->codec->codec_type = AVMEDIA_TYPE_VIDEO;
  330. st->codec->codec_id = AV_CODEC_ID_RAWVIDEO;
  331. st->sample_aspect_ratio = (AVRational){ aspectn, aspectd };
  332. st->codec->chroma_sample_location = chroma_sample_location;
  333. return 0;
  334. }
  335. static int yuv4_read_packet(AVFormatContext *s, AVPacket *pkt)
  336. {
  337. int i;
  338. char header[MAX_FRAME_HEADER+1];
  339. int packet_size, width, height, ret;
  340. AVStream *st = s->streams[0];
  341. struct frame_attributes *s1 = s->priv_data;
  342. for (i = 0; i < MAX_FRAME_HEADER; i++) {
  343. header[i] = avio_r8(s->pb);
  344. if (header[i] == '\n') {
  345. header[i + 1] = 0;
  346. break;
  347. }
  348. }
  349. if (s->pb->error)
  350. return s->pb->error;
  351. else if (s->pb->eof_reached)
  352. return AVERROR_EOF;
  353. else if (i == MAX_FRAME_HEADER)
  354. return AVERROR_INVALIDDATA;
  355. if (strncmp(header, Y4M_FRAME_MAGIC, strlen(Y4M_FRAME_MAGIC)))
  356. return AVERROR_INVALIDDATA;
  357. width = st->codec->width;
  358. height = st->codec->height;
  359. packet_size = avpicture_get_size(st->codec->pix_fmt, width, height);
  360. if (packet_size < 0)
  361. return packet_size;
  362. ret = av_get_packet(s->pb, pkt, packet_size);
  363. if (ret < 0)
  364. return ret;
  365. else if (ret != packet_size)
  366. return s->pb->eof_reached ? AVERROR_EOF : AVERROR(EIO);
  367. if (st->codec->coded_frame) {
  368. st->codec->coded_frame->interlaced_frame = s1->interlaced_frame;
  369. st->codec->coded_frame->top_field_first = s1->top_field_first;
  370. }
  371. pkt->stream_index = 0;
  372. return 0;
  373. }
  374. static int yuv4_probe(AVProbeData *pd)
  375. {
  376. /* check file header */
  377. if (strncmp(pd->buf, Y4M_MAGIC, sizeof(Y4M_MAGIC) - 1) == 0)
  378. return AVPROBE_SCORE_MAX;
  379. else
  380. return 0;
  381. }
  382. #if CONFIG_YUV4MPEGPIPE_DEMUXER
  383. AVInputFormat ff_yuv4mpegpipe_demuxer = {
  384. .name = "yuv4mpegpipe",
  385. .long_name = NULL_IF_CONFIG_SMALL("YUV4MPEG pipe"),
  386. .priv_data_size = sizeof(struct frame_attributes),
  387. .read_probe = yuv4_probe,
  388. .read_header = yuv4_read_header,
  389. .read_packet = yuv4_read_packet,
  390. .extensions = "y4m",
  391. };
  392. #endif