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.

430 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. .extensions = "y4m",
  159. .priv_data_size = sizeof(int),
  160. .audio_codec = CODEC_ID_NONE,
  161. .video_codec = CODEC_ID_RAWVIDEO,
  162. .write_header = yuv4_write_header,
  163. .write_packet = yuv4_write_packet,
  164. .flags = AVFMT_RAWPICTURE,
  165. };
  166. #endif
  167. /* Header size increased to allow room for optional flags */
  168. #define MAX_YUV4_HEADER 80
  169. #define MAX_FRAME_HEADER 80
  170. static int yuv4_read_header(AVFormatContext *s)
  171. {
  172. char header[MAX_YUV4_HEADER + 10]; // Include headroom for
  173. // the longest option
  174. char *tokstart, *tokend, *header_end;
  175. int i;
  176. AVIOContext *pb = s->pb;
  177. int width = -1, height = -1, raten = 0,
  178. rated = 0, aspectn = 0, aspectd = 0;
  179. enum PixelFormat pix_fmt = PIX_FMT_NONE, alt_pix_fmt = PIX_FMT_NONE;
  180. enum AVChromaLocation chroma_sample_location = AVCHROMA_LOC_UNSPECIFIED;
  181. AVStream *st;
  182. struct frame_attributes *s1 = s->priv_data;
  183. for (i = 0; i < MAX_YUV4_HEADER; i++) {
  184. header[i] = avio_r8(pb);
  185. if (header[i] == '\n') {
  186. header[i + 1] = 0x20; // Add a space after last option.
  187. // Makes parsing "444" vs "444alpha" easier.
  188. header[i + 2] = 0;
  189. break;
  190. }
  191. }
  192. if (i == MAX_YUV4_HEADER)
  193. return -1;
  194. if (strncmp(header, Y4M_MAGIC, strlen(Y4M_MAGIC)))
  195. return -1;
  196. s1->interlaced_frame = 0;
  197. s1->top_field_first = 0;
  198. header_end = &header[i + 1]; // Include space
  199. for (tokstart = &header[strlen(Y4M_MAGIC) + 1];
  200. tokstart < header_end; tokstart++) {
  201. if (*tokstart == 0x20)
  202. continue;
  203. switch (*tokstart++) {
  204. case 'W': // Width. Required.
  205. width = strtol(tokstart, &tokend, 10);
  206. tokstart = tokend;
  207. break;
  208. case 'H': // Height. Required.
  209. height = strtol(tokstart, &tokend, 10);
  210. tokstart = tokend;
  211. break;
  212. case 'C': // Color space
  213. if (strncmp("420jpeg", tokstart, 7) == 0) {
  214. pix_fmt = PIX_FMT_YUV420P;
  215. chroma_sample_location = AVCHROMA_LOC_CENTER;
  216. } else if (strncmp("420mpeg2", tokstart, 8) == 0) {
  217. pix_fmt = PIX_FMT_YUV420P;
  218. chroma_sample_location = AVCHROMA_LOC_LEFT;
  219. } else if (strncmp("420paldv", tokstart, 8) == 0) {
  220. pix_fmt = PIX_FMT_YUV420P;
  221. chroma_sample_location = AVCHROMA_LOC_TOPLEFT;
  222. } else if (strncmp("420", tokstart, 3) == 0) {
  223. pix_fmt = PIX_FMT_YUV420P;
  224. chroma_sample_location = AVCHROMA_LOC_CENTER;
  225. } else if (strncmp("411", tokstart, 3) == 0)
  226. pix_fmt = PIX_FMT_YUV411P;
  227. else if (strncmp("422", tokstart, 3) == 0)
  228. pix_fmt = PIX_FMT_YUV422P;
  229. else if (strncmp("444alpha", tokstart, 8) == 0 ) {
  230. av_log(s, AV_LOG_ERROR, "Cannot handle 4:4:4:4 "
  231. "YUV4MPEG stream.\n");
  232. return -1;
  233. } else if (strncmp("444", tokstart, 3) == 0)
  234. pix_fmt = PIX_FMT_YUV444P;
  235. else if (strncmp("mono", tokstart, 4) == 0) {
  236. pix_fmt = PIX_FMT_GRAY8;
  237. } else {
  238. av_log(s, AV_LOG_ERROR, "YUV4MPEG stream contains an unknown "
  239. "pixel format.\n");
  240. return -1;
  241. }
  242. while (tokstart < header_end && *tokstart != 0x20)
  243. tokstart++;
  244. break;
  245. case 'I': // Interlace type
  246. switch (*tokstart++){
  247. case '?':
  248. break;
  249. case 'p':
  250. s1->interlaced_frame = 0;
  251. break;
  252. case 't':
  253. s1->interlaced_frame = 1;
  254. s1->top_field_first = 1;
  255. break;
  256. case 'b':
  257. s1->interlaced_frame = 1;
  258. s1->top_field_first = 0;
  259. break;
  260. case 'm':
  261. av_log(s, AV_LOG_ERROR, "YUV4MPEG stream contains mixed "
  262. "interlaced and non-interlaced frames.\n");
  263. return -1;
  264. default:
  265. av_log(s, AV_LOG_ERROR, "YUV4MPEG has invalid header.\n");
  266. return -1;
  267. }
  268. break;
  269. case 'F': // Frame rate
  270. sscanf(tokstart, "%d:%d", &raten, &rated); // 0:0 if unknown
  271. while (tokstart < header_end && *tokstart != 0x20)
  272. tokstart++;
  273. break;
  274. case 'A': // Pixel aspect
  275. sscanf(tokstart, "%d:%d", &aspectn, &aspectd); // 0:0 if unknown
  276. while (tokstart < header_end && *tokstart != 0x20)
  277. tokstart++;
  278. break;
  279. case 'X': // Vendor extensions
  280. if (strncmp("YSCSS=", tokstart, 6) == 0) {
  281. // Older nonstandard pixel format representation
  282. tokstart += 6;
  283. if (strncmp("420JPEG", tokstart, 7) == 0)
  284. alt_pix_fmt = PIX_FMT_YUV420P;
  285. else if (strncmp("420MPEG2", tokstart, 8) == 0)
  286. alt_pix_fmt = PIX_FMT_YUV420P;
  287. else if (strncmp("420PALDV", tokstart, 8) == 0)
  288. alt_pix_fmt = PIX_FMT_YUV420P;
  289. else if (strncmp("411", tokstart, 3) == 0)
  290. alt_pix_fmt = PIX_FMT_YUV411P;
  291. else if (strncmp("422", tokstart, 3) == 0)
  292. alt_pix_fmt = PIX_FMT_YUV422P;
  293. else if (strncmp("444", tokstart, 3) == 0)
  294. alt_pix_fmt = PIX_FMT_YUV444P;
  295. }
  296. while (tokstart < header_end && *tokstart != 0x20)
  297. tokstart++;
  298. break;
  299. }
  300. }
  301. if (width == -1 || height == -1) {
  302. av_log(s, AV_LOG_ERROR, "YUV4MPEG has invalid header.\n");
  303. return -1;
  304. }
  305. if (pix_fmt == PIX_FMT_NONE) {
  306. if (alt_pix_fmt == PIX_FMT_NONE)
  307. pix_fmt = PIX_FMT_YUV420P;
  308. else
  309. pix_fmt = alt_pix_fmt;
  310. }
  311. if (raten <= 0 || rated <= 0) {
  312. // Frame rate unknown
  313. raten = 25;
  314. rated = 1;
  315. }
  316. if (aspectn == 0 && aspectd == 0) {
  317. // Pixel aspect unknown
  318. aspectd = 1;
  319. }
  320. st = avformat_new_stream(s, NULL);
  321. if (!st)
  322. return AVERROR(ENOMEM);
  323. st->codec->width = width;
  324. st->codec->height = height;
  325. av_reduce(&raten, &rated, raten, rated, (1UL << 31) - 1);
  326. avpriv_set_pts_info(st, 64, rated, raten);
  327. st->codec->pix_fmt = pix_fmt;
  328. st->codec->codec_type = AVMEDIA_TYPE_VIDEO;
  329. st->codec->codec_id = CODEC_ID_RAWVIDEO;
  330. st->sample_aspect_ratio = (AVRational){ aspectn, aspectd };
  331. st->codec->chroma_sample_location = chroma_sample_location;
  332. return 0;
  333. }
  334. static int yuv4_read_packet(AVFormatContext *s, AVPacket *pkt)
  335. {
  336. int i;
  337. char header[MAX_FRAME_HEADER+1];
  338. int packet_size, width, height;
  339. AVStream *st = s->streams[0];
  340. struct frame_attributes *s1 = s->priv_data;
  341. for (i = 0; i < MAX_FRAME_HEADER; i++) {
  342. header[i] = avio_r8(s->pb);
  343. if (header[i] == '\n') {
  344. header[i + 1] = 0;
  345. break;
  346. }
  347. }
  348. if (i == MAX_FRAME_HEADER)
  349. return -1;
  350. if (strncmp(header, Y4M_FRAME_MAGIC, strlen(Y4M_FRAME_MAGIC)))
  351. return -1;
  352. width = st->codec->width;
  353. height = st->codec->height;
  354. packet_size = avpicture_get_size(st->codec->pix_fmt, width, height);
  355. if (packet_size < 0)
  356. return -1;
  357. if (av_get_packet(s->pb, pkt, packet_size) != packet_size)
  358. return AVERROR(EIO);
  359. if (st->codec->coded_frame) {
  360. st->codec->coded_frame->interlaced_frame = s1->interlaced_frame;
  361. st->codec->coded_frame->top_field_first = s1->top_field_first;
  362. }
  363. pkt->stream_index = 0;
  364. return 0;
  365. }
  366. static int yuv4_probe(AVProbeData *pd)
  367. {
  368. /* check file header */
  369. if (strncmp(pd->buf, Y4M_MAGIC, sizeof(Y4M_MAGIC) - 1) == 0)
  370. return AVPROBE_SCORE_MAX;
  371. else
  372. return 0;
  373. }
  374. #if CONFIG_YUV4MPEGPIPE_DEMUXER
  375. AVInputFormat ff_yuv4mpegpipe_demuxer = {
  376. .name = "yuv4mpegpipe",
  377. .long_name = NULL_IF_CONFIG_SMALL("YUV4MPEG pipe format"),
  378. .priv_data_size = sizeof(struct frame_attributes),
  379. .read_probe = yuv4_probe,
  380. .read_header = yuv4_read_header,
  381. .read_packet = yuv4_read_packet,
  382. .extensions = "y4m",
  383. };
  384. #endif