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.

428 lines
14KB

  1. /*
  2. * YUV4MPEG format
  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 "avformat.h"
  22. #include "internal.h"
  23. #define Y4M_MAGIC "YUV4MPEG2"
  24. #define Y4M_FRAME_MAGIC "FRAME"
  25. #define Y4M_LINE_MAX 256
  26. struct frame_attributes {
  27. int interlaced_frame;
  28. int top_field_first;
  29. };
  30. #if CONFIG_YUV4MPEGPIPE_MUXER
  31. static int yuv4_generate_header(AVFormatContext *s, char* buf)
  32. {
  33. AVStream *st;
  34. int width, height;
  35. int raten, rated, aspectn, aspectd, n;
  36. char inter;
  37. const char *colorspace = "";
  38. st = s->streams[0];
  39. width = st->codec->width;
  40. height = st->codec->height;
  41. av_reduce(&raten, &rated, st->codec->time_base.den,
  42. st->codec->time_base.num, (1UL << 31) - 1);
  43. aspectn = st->sample_aspect_ratio.num;
  44. aspectd = st->sample_aspect_ratio.den;
  45. if (aspectn == 0 && aspectd == 1)
  46. aspectd = 0; // 0:0 means unknown
  47. inter = 'p'; /* progressive is the default */
  48. if (st->codec->coded_frame && st->codec->coded_frame->interlaced_frame)
  49. inter = st->codec->coded_frame->top_field_first ? 't' : 'b';
  50. switch (st->codec->pix_fmt) {
  51. case PIX_FMT_GRAY8:
  52. colorspace = " Cmono";
  53. break;
  54. case PIX_FMT_YUV411P:
  55. colorspace = " C411 XYSCSS=411";
  56. break;
  57. case PIX_FMT_YUV420P:
  58. switch (st->codec->chroma_sample_location) {
  59. case AVCHROMA_LOC_TOPLEFT: colorspace = " C420paldv XYSCSS=420PALDV"; break;
  60. case AVCHROMA_LOC_LEFT: colorspace = " C420mpeg2 XYSCSS=420MPEG2"; break;
  61. default: colorspace = " C420jpeg XYSCSS=420JPEG"; break;
  62. }
  63. break;
  64. case PIX_FMT_YUV422P:
  65. colorspace = " C422 XYSCSS=422";
  66. break;
  67. case PIX_FMT_YUV444P:
  68. colorspace = " C444 XYSCSS=444";
  69. break;
  70. }
  71. /* construct stream header, if this is the first frame */
  72. n = snprintf(buf, Y4M_LINE_MAX, "%s W%d H%d F%d:%d I%c A%d:%d%s\n",
  73. Y4M_MAGIC, width, height, raten, rated, inter,
  74. aspectn, aspectd, colorspace);
  75. return n;
  76. }
  77. static int yuv4_write_packet(AVFormatContext *s, AVPacket *pkt)
  78. {
  79. AVStream *st = s->streams[pkt->stream_index];
  80. AVIOContext *pb = s->pb;
  81. AVPicture *picture;
  82. int* first_pkt = s->priv_data;
  83. int width, height, h_chroma_shift, v_chroma_shift;
  84. int i;
  85. char buf2[Y4M_LINE_MAX + 1];
  86. char buf1[20];
  87. uint8_t *ptr, *ptr1, *ptr2;
  88. picture = (AVPicture *)pkt->data;
  89. /* for the first packet we have to output the header as well */
  90. if (*first_pkt) {
  91. *first_pkt = 0;
  92. if (yuv4_generate_header(s, buf2) < 0) {
  93. av_log(s, AV_LOG_ERROR,
  94. "Error. YUV4MPEG stream header write failed.\n");
  95. return AVERROR(EIO);
  96. } else {
  97. avio_write(pb, buf2, strlen(buf2));
  98. }
  99. }
  100. /* construct frame header */
  101. snprintf(buf1, sizeof(buf1), "%s\n", Y4M_FRAME_MAGIC);
  102. avio_write(pb, buf1, strlen(buf1));
  103. width = st->codec->width;
  104. height = st->codec->height;
  105. ptr = picture->data[0];
  106. for (i = 0; i < height; i++) {
  107. avio_write(pb, ptr, width);
  108. ptr += picture->linesize[0];
  109. }
  110. if (st->codec->pix_fmt != PIX_FMT_GRAY8) {
  111. // Adjust for smaller Cb and Cr planes
  112. avcodec_get_chroma_sub_sample(st->codec->pix_fmt, &h_chroma_shift,
  113. &v_chroma_shift);
  114. width >>= h_chroma_shift;
  115. height >>= v_chroma_shift;
  116. ptr1 = picture->data[1];
  117. ptr2 = picture->data[2];
  118. for (i = 0; i < height; i++) { /* Cb */
  119. avio_write(pb, ptr1, width);
  120. ptr1 += picture->linesize[1];
  121. }
  122. for (i = 0; i < height; i++) { /* Cr */
  123. avio_write(pb, ptr2, width);
  124. ptr2 += picture->linesize[2];
  125. }
  126. }
  127. avio_flush(pb);
  128. return 0;
  129. }
  130. static int yuv4_write_header(AVFormatContext *s)
  131. {
  132. int *first_pkt = s->priv_data;
  133. if (s->nb_streams != 1)
  134. return AVERROR(EIO);
  135. if (s->streams[0]->codec->codec_id != CODEC_ID_RAWVIDEO) {
  136. av_log(s, AV_LOG_ERROR,
  137. "A non-rawvideo stream was selected, but yuv4mpeg only handles rawvideo streams\n");
  138. return AVERROR(EINVAL);
  139. }
  140. if (s->streams[0]->codec->pix_fmt == 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 != PIX_FMT_YUV420P) &&
  144. (s->streams[0]->codec->pix_fmt != PIX_FMT_YUV422P) &&
  145. (s->streams[0]->codec->pix_fmt != PIX_FMT_GRAY8) &&
  146. (s->streams[0]->codec->pix_fmt != 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 format"),
  158. .mime_type = "",
  159. .extensions = "y4m",
  160. .priv_data_size = sizeof(int),
  161. .audio_codec = CODEC_ID_NONE,
  162. .video_codec = 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, AVFormatParameters *ap)
  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 PixelFormat pix_fmt = PIX_FMT_NONE, alt_pix_fmt = 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 = PIX_FMT_YUV420P;
  216. chroma_sample_location = AVCHROMA_LOC_CENTER;
  217. } else if (strncmp("420mpeg2", tokstart, 8) == 0) {
  218. pix_fmt = PIX_FMT_YUV420P;
  219. chroma_sample_location = AVCHROMA_LOC_LEFT;
  220. } else if (strncmp("420paldv", tokstart, 8) == 0) {
  221. pix_fmt = PIX_FMT_YUV420P;
  222. chroma_sample_location = AVCHROMA_LOC_TOPLEFT;
  223. } else if (strncmp("411", tokstart, 3) == 0)
  224. pix_fmt = PIX_FMT_YUV411P;
  225. else if (strncmp("422", tokstart, 3) == 0)
  226. pix_fmt = PIX_FMT_YUV422P;
  227. else if (strncmp("444alpha", tokstart, 8) == 0 ) {
  228. av_log(s, AV_LOG_ERROR, "Cannot handle 4:4:4:4 "
  229. "YUV4MPEG stream.\n");
  230. return -1;
  231. } else if (strncmp("444", tokstart, 3) == 0)
  232. pix_fmt = PIX_FMT_YUV444P;
  233. else if (strncmp("mono", tokstart, 4) == 0) {
  234. pix_fmt = PIX_FMT_GRAY8;
  235. } else {
  236. av_log(s, AV_LOG_ERROR, "YUV4MPEG stream contains an unknown "
  237. "pixel format.\n");
  238. return -1;
  239. }
  240. while (tokstart < header_end && *tokstart != 0x20)
  241. tokstart++;
  242. break;
  243. case 'I': // Interlace type
  244. switch (*tokstart++){
  245. case '?':
  246. break;
  247. case 'p':
  248. s1->interlaced_frame = 0;
  249. break;
  250. case 't':
  251. s1->interlaced_frame = 1;
  252. s1->top_field_first = 1;
  253. break;
  254. case 'b':
  255. s1->interlaced_frame = 1;
  256. s1->top_field_first = 0;
  257. break;
  258. case 'm':
  259. av_log(s, AV_LOG_ERROR, "YUV4MPEG stream contains mixed "
  260. "interlaced and non-interlaced frames.\n");
  261. return -1;
  262. default:
  263. av_log(s, AV_LOG_ERROR, "YUV4MPEG has invalid header.\n");
  264. return -1;
  265. }
  266. break;
  267. case 'F': // Frame rate
  268. sscanf(tokstart, "%d:%d", &raten, &rated); // 0:0 if unknown
  269. while (tokstart < header_end && *tokstart != 0x20)
  270. tokstart++;
  271. break;
  272. case 'A': // Pixel aspect
  273. sscanf(tokstart, "%d:%d", &aspectn, &aspectd); // 0:0 if unknown
  274. while (tokstart < header_end && *tokstart != 0x20)
  275. tokstart++;
  276. break;
  277. case 'X': // Vendor extensions
  278. if (strncmp("YSCSS=", tokstart, 6) == 0) {
  279. // Older nonstandard pixel format representation
  280. tokstart += 6;
  281. if (strncmp("420JPEG", tokstart, 7) == 0)
  282. alt_pix_fmt = PIX_FMT_YUV420P;
  283. else if (strncmp("420MPEG2", tokstart, 8) == 0)
  284. alt_pix_fmt = PIX_FMT_YUV420P;
  285. else if (strncmp("420PALDV", tokstart, 8) == 0)
  286. alt_pix_fmt = PIX_FMT_YUV420P;
  287. else if (strncmp("411", tokstart, 3) == 0)
  288. alt_pix_fmt = PIX_FMT_YUV411P;
  289. else if (strncmp("422", tokstart, 3) == 0)
  290. alt_pix_fmt = PIX_FMT_YUV422P;
  291. else if (strncmp("444", tokstart, 3) == 0)
  292. alt_pix_fmt = PIX_FMT_YUV444P;
  293. }
  294. while (tokstart < header_end && *tokstart != 0x20)
  295. tokstart++;
  296. break;
  297. }
  298. }
  299. if (width == -1 || height == -1) {
  300. av_log(s, AV_LOG_ERROR, "YUV4MPEG has invalid header.\n");
  301. return -1;
  302. }
  303. if (pix_fmt == PIX_FMT_NONE) {
  304. if (alt_pix_fmt == PIX_FMT_NONE)
  305. pix_fmt = PIX_FMT_YUV420P;
  306. else
  307. pix_fmt = alt_pix_fmt;
  308. }
  309. if (raten <= 0 || rated <= 0) {
  310. // Frame rate unknown
  311. raten = 25;
  312. rated = 1;
  313. }
  314. if (aspectn == 0 && aspectd == 0) {
  315. // Pixel aspect unknown
  316. aspectd = 1;
  317. }
  318. st = avformat_new_stream(s, NULL);
  319. if (!st)
  320. return AVERROR(ENOMEM);
  321. st->codec->width = width;
  322. st->codec->height = height;
  323. av_reduce(&raten, &rated, raten, rated, (1UL << 31) - 1);
  324. avpriv_set_pts_info(st, 64, rated, raten);
  325. st->codec->pix_fmt = pix_fmt;
  326. st->codec->codec_type = AVMEDIA_TYPE_VIDEO;
  327. st->codec->codec_id = CODEC_ID_RAWVIDEO;
  328. st->sample_aspect_ratio = (AVRational){ aspectn, aspectd };
  329. st->codec->chroma_sample_location = chroma_sample_location;
  330. return 0;
  331. }
  332. static int yuv4_read_packet(AVFormatContext *s, AVPacket *pkt)
  333. {
  334. int i;
  335. char header[MAX_FRAME_HEADER+1];
  336. int packet_size, width, height;
  337. AVStream *st = s->streams[0];
  338. struct frame_attributes *s1 = s->priv_data;
  339. for (i = 0; i < MAX_FRAME_HEADER; i++) {
  340. header[i] = avio_r8(s->pb);
  341. if (header[i] == '\n') {
  342. header[i + 1] = 0;
  343. break;
  344. }
  345. }
  346. if (i == MAX_FRAME_HEADER)
  347. return -1;
  348. if (strncmp(header, Y4M_FRAME_MAGIC, strlen(Y4M_FRAME_MAGIC)))
  349. return -1;
  350. width = st->codec->width;
  351. height = st->codec->height;
  352. packet_size = avpicture_get_size(st->codec->pix_fmt, width, height);
  353. if (packet_size < 0)
  354. return -1;
  355. if (av_get_packet(s->pb, pkt, packet_size) != packet_size)
  356. return AVERROR(EIO);
  357. if (st->codec->coded_frame) {
  358. st->codec->coded_frame->interlaced_frame = s1->interlaced_frame;
  359. st->codec->coded_frame->top_field_first = s1->top_field_first;
  360. }
  361. pkt->stream_index = 0;
  362. return 0;
  363. }
  364. static int yuv4_probe(AVProbeData *pd)
  365. {
  366. /* check file header */
  367. if (strncmp(pd->buf, Y4M_MAGIC, sizeof(Y4M_MAGIC) - 1) == 0)
  368. return AVPROBE_SCORE_MAX;
  369. else
  370. return 0;
  371. }
  372. #if CONFIG_YUV4MPEGPIPE_DEMUXER
  373. AVInputFormat ff_yuv4mpegpipe_demuxer = {
  374. .name = "yuv4mpegpipe",
  375. .long_name = NULL_IF_CONFIG_SMALL("YUV4MPEG pipe format"),
  376. .priv_data_size = sizeof(struct frame_attributes),
  377. .read_probe = yuv4_probe,
  378. .read_header = yuv4_read_header,
  379. .read_packet = yuv4_read_packet,
  380. .extensions = "y4m"
  381. };
  382. #endif