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.

618 lines
21KB

  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. #include "libavutil/pixdesc.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_GRAY16:
  56. colorspace = " Cmono16";
  57. break;
  58. case AV_PIX_FMT_YUV411P:
  59. colorspace = " C411 XYSCSS=411";
  60. break;
  61. case AV_PIX_FMT_YUV420P:
  62. switch (st->codec->chroma_sample_location) {
  63. case AVCHROMA_LOC_TOPLEFT: colorspace = " C420paldv XYSCSS=420PALDV"; break;
  64. case AVCHROMA_LOC_LEFT: colorspace = " C420mpeg2 XYSCSS=420MPEG2"; break;
  65. default: colorspace = " C420jpeg XYSCSS=420JPEG"; break;
  66. }
  67. break;
  68. case AV_PIX_FMT_YUV422P:
  69. colorspace = " C422 XYSCSS=422";
  70. break;
  71. case AV_PIX_FMT_YUV444P:
  72. colorspace = " C444 XYSCSS=444";
  73. break;
  74. case AV_PIX_FMT_YUV420P9:
  75. colorspace = " C420p9 XYSCSS=420P9";
  76. break;
  77. case AV_PIX_FMT_YUV422P9:
  78. colorspace = " C422p9 XYSCSS=422P9";
  79. break;
  80. case AV_PIX_FMT_YUV444P9:
  81. colorspace = " C444p9 XYSCSS=444P9";
  82. break;
  83. case AV_PIX_FMT_YUV420P10:
  84. colorspace = " C420p10 XYSCSS=420P10";
  85. break;
  86. case AV_PIX_FMT_YUV422P10:
  87. colorspace = " C422p10 XYSCSS=422P10";
  88. break;
  89. case AV_PIX_FMT_YUV444P10:
  90. colorspace = " C444p10 XYSCSS=444P10";
  91. break;
  92. case AV_PIX_FMT_YUV420P12:
  93. colorspace = " C420p12 XYSCSS=420P12";
  94. break;
  95. case AV_PIX_FMT_YUV422P12:
  96. colorspace = " C422p12 XYSCSS=422P12";
  97. break;
  98. case AV_PIX_FMT_YUV444P12:
  99. colorspace = " C444p12 XYSCSS=444P12";
  100. break;
  101. case AV_PIX_FMT_YUV420P14:
  102. colorspace = " C420p14 XYSCSS=420P14";
  103. break;
  104. case AV_PIX_FMT_YUV422P14:
  105. colorspace = " C422p14 XYSCSS=422P14";
  106. break;
  107. case AV_PIX_FMT_YUV444P14:
  108. colorspace = " C444p14 XYSCSS=444P14";
  109. break;
  110. case AV_PIX_FMT_YUV420P16:
  111. colorspace = " C420p16 XYSCSS=420P16";
  112. break;
  113. case AV_PIX_FMT_YUV422P16:
  114. colorspace = " C422p16 XYSCSS=422P16";
  115. break;
  116. case AV_PIX_FMT_YUV444P16:
  117. colorspace = " C444p16 XYSCSS=444P16";
  118. break;
  119. }
  120. /* construct stream header, if this is the first frame */
  121. n = snprintf(buf, Y4M_LINE_MAX, "%s W%d H%d F%d:%d I%c A%d:%d%s\n",
  122. Y4M_MAGIC, width, height, raten, rated, inter,
  123. aspectn, aspectd, colorspace);
  124. return n;
  125. }
  126. static int yuv4_write_packet(AVFormatContext *s, AVPacket *pkt)
  127. {
  128. AVStream *st = s->streams[pkt->stream_index];
  129. AVIOContext *pb = s->pb;
  130. AVPicture *picture, picture_tmp;
  131. int* first_pkt = s->priv_data;
  132. int width, height, h_chroma_shift, v_chroma_shift;
  133. int i;
  134. char buf2[Y4M_LINE_MAX + 1];
  135. char buf1[20];
  136. uint8_t *ptr, *ptr1, *ptr2;
  137. memcpy(&picture_tmp, pkt->data, sizeof(AVPicture));
  138. picture = &picture_tmp;
  139. /* for the first packet we have to output the header as well */
  140. if (*first_pkt) {
  141. *first_pkt = 0;
  142. if (yuv4_generate_header(s, buf2) < 0) {
  143. av_log(s, AV_LOG_ERROR,
  144. "Error. YUV4MPEG stream header write failed.\n");
  145. return AVERROR(EIO);
  146. } else {
  147. avio_write(pb, buf2, strlen(buf2));
  148. }
  149. }
  150. /* construct frame header */
  151. snprintf(buf1, sizeof(buf1), "%s\n", Y4M_FRAME_MAGIC);
  152. avio_write(pb, buf1, strlen(buf1));
  153. width = st->codec->width;
  154. height = st->codec->height;
  155. ptr = picture->data[0];
  156. switch (st->codec->pix_fmt) {
  157. case AV_PIX_FMT_GRAY8:
  158. case AV_PIX_FMT_YUV411P:
  159. case AV_PIX_FMT_YUV420P:
  160. case AV_PIX_FMT_YUV422P:
  161. case AV_PIX_FMT_YUV444P:
  162. break;
  163. case AV_PIX_FMT_GRAY16:
  164. case AV_PIX_FMT_YUV420P9:
  165. case AV_PIX_FMT_YUV422P9:
  166. case AV_PIX_FMT_YUV444P9:
  167. case AV_PIX_FMT_YUV420P10:
  168. case AV_PIX_FMT_YUV422P10:
  169. case AV_PIX_FMT_YUV444P10:
  170. case AV_PIX_FMT_YUV420P12:
  171. case AV_PIX_FMT_YUV422P12:
  172. case AV_PIX_FMT_YUV444P12:
  173. case AV_PIX_FMT_YUV420P14:
  174. case AV_PIX_FMT_YUV422P14:
  175. case AV_PIX_FMT_YUV444P14:
  176. case AV_PIX_FMT_YUV420P16:
  177. case AV_PIX_FMT_YUV422P16:
  178. case AV_PIX_FMT_YUV444P16:
  179. width *= 2;
  180. break;
  181. default:
  182. av_log(s, AV_LOG_ERROR, "The pixel format '%s' is not supported.\n",
  183. av_get_pix_fmt_name(st->codec->pix_fmt));
  184. return AVERROR(EINVAL);
  185. }
  186. for (i = 0; i < height; i++) {
  187. avio_write(pb, ptr, width);
  188. ptr += picture->linesize[0];
  189. }
  190. if (st->codec->pix_fmt != AV_PIX_FMT_GRAY8 &&
  191. st->codec->pix_fmt != AV_PIX_FMT_GRAY16) {
  192. // Adjust for smaller Cb and Cr planes
  193. avcodec_get_chroma_sub_sample(st->codec->pix_fmt, &h_chroma_shift,
  194. &v_chroma_shift);
  195. width >>= h_chroma_shift;
  196. height >>= v_chroma_shift;
  197. ptr1 = picture->data[1];
  198. ptr2 = picture->data[2];
  199. for (i = 0; i < height; i++) { /* Cb */
  200. avio_write(pb, ptr1, width);
  201. ptr1 += picture->linesize[1];
  202. }
  203. for (i = 0; i < height; i++) { /* Cr */
  204. avio_write(pb, ptr2, width);
  205. ptr2 += picture->linesize[2];
  206. }
  207. }
  208. avio_flush(pb);
  209. return 0;
  210. }
  211. static int yuv4_write_header(AVFormatContext *s)
  212. {
  213. int *first_pkt = s->priv_data;
  214. if (s->nb_streams != 1)
  215. return AVERROR(EIO);
  216. if (s->streams[0]->codec->codec_id != AV_CODEC_ID_RAWVIDEO) {
  217. av_log(s, AV_LOG_ERROR, "ERROR: Only rawvideo supported.\n");
  218. return AVERROR_INVALIDDATA;
  219. }
  220. switch (s->streams[0]->codec->pix_fmt) {
  221. case AV_PIX_FMT_YUV411P:
  222. av_log(s, AV_LOG_WARNING, "Warning: generating rarely used 4:1:1 YUV "
  223. "stream, some mjpegtools might not work.\n");
  224. break;
  225. case AV_PIX_FMT_GRAY8:
  226. case AV_PIX_FMT_GRAY16:
  227. case AV_PIX_FMT_YUV420P:
  228. case AV_PIX_FMT_YUV422P:
  229. case AV_PIX_FMT_YUV444P:
  230. break;
  231. case AV_PIX_FMT_YUV420P9:
  232. case AV_PIX_FMT_YUV422P9:
  233. case AV_PIX_FMT_YUV444P9:
  234. case AV_PIX_FMT_YUV420P10:
  235. case AV_PIX_FMT_YUV422P10:
  236. case AV_PIX_FMT_YUV444P10:
  237. case AV_PIX_FMT_YUV420P12:
  238. case AV_PIX_FMT_YUV422P12:
  239. case AV_PIX_FMT_YUV444P12:
  240. case AV_PIX_FMT_YUV420P14:
  241. case AV_PIX_FMT_YUV422P14:
  242. case AV_PIX_FMT_YUV444P14:
  243. case AV_PIX_FMT_YUV420P16:
  244. case AV_PIX_FMT_YUV422P16:
  245. case AV_PIX_FMT_YUV444P16:
  246. if (s->streams[0]->codec->strict_std_compliance >= FF_COMPLIANCE_NORMAL) {
  247. av_log(s, AV_LOG_ERROR, "'%s' is not a official yuv4mpegpipe pixel format. "
  248. "Use '-strict -1' to encode to this pixel format.\n",
  249. av_get_pix_fmt_name(s->streams[0]->codec->pix_fmt));
  250. return AVERROR(EINVAL);
  251. }
  252. av_log(s, AV_LOG_WARNING, "Warning: generating non standart YUV stream. "
  253. "Mjpegtools will not work.\n");
  254. break;
  255. default:
  256. av_log(s, AV_LOG_ERROR, "ERROR: yuv4mpeg can only handle "
  257. "yuv444p, yuv422p, yuv420p, yuv411p and gray8 pixel formats. "
  258. "And using 'strict -1' also yuv444p9, yuv422p9, yuv420p9, "
  259. "yuv444p10, yuv422p10, yuv420p10, "
  260. "yuv444p12, yuv422p12, yuv420p12, "
  261. "yuv444p14, yuv422p14, yuv420p14, "
  262. "yuv444p16, yuv422p16, yuv420p16 "
  263. "and gray16 pixel formats. "
  264. "Use -pix_fmt to select one.\n");
  265. return AVERROR(EIO);
  266. }
  267. *first_pkt = 1;
  268. return 0;
  269. }
  270. AVOutputFormat ff_yuv4mpegpipe_muxer = {
  271. .name = "yuv4mpegpipe",
  272. .long_name = NULL_IF_CONFIG_SMALL("YUV4MPEG pipe"),
  273. .extensions = "y4m",
  274. .priv_data_size = sizeof(int),
  275. .audio_codec = AV_CODEC_ID_NONE,
  276. .video_codec = AV_CODEC_ID_RAWVIDEO,
  277. .write_header = yuv4_write_header,
  278. .write_packet = yuv4_write_packet,
  279. .flags = AVFMT_RAWPICTURE,
  280. };
  281. #endif
  282. /* Header size increased to allow room for optional flags */
  283. #define MAX_YUV4_HEADER 80
  284. #define MAX_FRAME_HEADER 80
  285. static int yuv4_read_header(AVFormatContext *s)
  286. {
  287. char header[MAX_YUV4_HEADER + 10]; // Include headroom for
  288. // the longest option
  289. char *tokstart, *tokend, *header_end;
  290. int i;
  291. AVIOContext *pb = s->pb;
  292. int width = -1, height = -1, raten = 0,
  293. rated = 0, aspectn = 0, aspectd = 0;
  294. enum AVPixelFormat pix_fmt = AV_PIX_FMT_NONE, alt_pix_fmt = AV_PIX_FMT_NONE;
  295. enum AVChromaLocation chroma_sample_location = AVCHROMA_LOC_UNSPECIFIED;
  296. AVStream *st;
  297. struct frame_attributes *s1 = s->priv_data;
  298. for (i = 0; i < MAX_YUV4_HEADER; i++) {
  299. header[i] = avio_r8(pb);
  300. if (header[i] == '\n') {
  301. header[i + 1] = 0x20; // Add a space after last option.
  302. // Makes parsing "444" vs "444alpha" easier.
  303. header[i + 2] = 0;
  304. break;
  305. }
  306. }
  307. if (i == MAX_YUV4_HEADER)
  308. return -1;
  309. if (strncmp(header, Y4M_MAGIC, strlen(Y4M_MAGIC)))
  310. return -1;
  311. s1->interlaced_frame = 0;
  312. s1->top_field_first = 0;
  313. header_end = &header[i + 1]; // Include space
  314. for (tokstart = &header[strlen(Y4M_MAGIC) + 1];
  315. tokstart < header_end; tokstart++) {
  316. if (*tokstart == 0x20)
  317. continue;
  318. switch (*tokstart++) {
  319. case 'W': // Width. Required.
  320. width = strtol(tokstart, &tokend, 10);
  321. tokstart = tokend;
  322. break;
  323. case 'H': // Height. Required.
  324. height = strtol(tokstart, &tokend, 10);
  325. tokstart = tokend;
  326. break;
  327. case 'C': // Color space
  328. if (strncmp("420jpeg", tokstart, 7) == 0) {
  329. pix_fmt = AV_PIX_FMT_YUV420P;
  330. chroma_sample_location = AVCHROMA_LOC_CENTER;
  331. } else if (strncmp("420mpeg2", tokstart, 8) == 0) {
  332. pix_fmt = AV_PIX_FMT_YUV420P;
  333. chroma_sample_location = AVCHROMA_LOC_LEFT;
  334. } else if (strncmp("420paldv", tokstart, 8) == 0) {
  335. pix_fmt = AV_PIX_FMT_YUV420P;
  336. chroma_sample_location = AVCHROMA_LOC_TOPLEFT;
  337. } else if (strncmp("420p16", tokstart, 6) == 0) {
  338. pix_fmt = AV_PIX_FMT_YUV420P16;
  339. } else if (strncmp("422p16", tokstart, 6) == 0) {
  340. pix_fmt = AV_PIX_FMT_YUV422P16;
  341. } else if (strncmp("444p16", tokstart, 6) == 0) {
  342. pix_fmt = AV_PIX_FMT_YUV444P16;
  343. } else if (strncmp("420p14", tokstart, 6) == 0) {
  344. pix_fmt = AV_PIX_FMT_YUV420P14;
  345. } else if (strncmp("422p14", tokstart, 6) == 0) {
  346. pix_fmt = AV_PIX_FMT_YUV422P14;
  347. } else if (strncmp("444p14", tokstart, 6) == 0) {
  348. pix_fmt = AV_PIX_FMT_YUV444P14;
  349. } else if (strncmp("420p12", tokstart, 6) == 0) {
  350. pix_fmt = AV_PIX_FMT_YUV420P12;
  351. } else if (strncmp("422p12", tokstart, 6) == 0) {
  352. pix_fmt = AV_PIX_FMT_YUV422P12;
  353. } else if (strncmp("444p12", tokstart, 6) == 0) {
  354. pix_fmt = AV_PIX_FMT_YUV444P12;
  355. } else if (strncmp("420p10", tokstart, 6) == 0) {
  356. pix_fmt = AV_PIX_FMT_YUV420P10;
  357. } else if (strncmp("422p10", tokstart, 6) == 0) {
  358. pix_fmt = AV_PIX_FMT_YUV422P10;
  359. } else if (strncmp("444p10", tokstart, 6) == 0) {
  360. pix_fmt = AV_PIX_FMT_YUV444P10;
  361. } else if (strncmp("420p9", tokstart, 5) == 0) {
  362. pix_fmt = AV_PIX_FMT_YUV420P9;
  363. } else if (strncmp("422p9", tokstart, 5) == 0) {
  364. pix_fmt = AV_PIX_FMT_YUV422P9;
  365. } else if (strncmp("444p9", tokstart, 5) == 0) {
  366. pix_fmt = AV_PIX_FMT_YUV444P9;
  367. } else if (strncmp("420", tokstart, 3) == 0) {
  368. pix_fmt = AV_PIX_FMT_YUV420P;
  369. chroma_sample_location = AVCHROMA_LOC_CENTER;
  370. } else if (strncmp("411", tokstart, 3) == 0) {
  371. pix_fmt = AV_PIX_FMT_YUV411P;
  372. } else if (strncmp("422", tokstart, 3) == 0) {
  373. pix_fmt = AV_PIX_FMT_YUV422P;
  374. } else if (strncmp("444alpha", tokstart, 8) == 0 ) {
  375. av_log(s, AV_LOG_ERROR, "Cannot handle 4:4:4:4 "
  376. "YUV4MPEG stream.\n");
  377. return -1;
  378. } else if (strncmp("444", tokstart, 3) == 0) {
  379. pix_fmt = AV_PIX_FMT_YUV444P;
  380. } else if (strncmp("mono16", tokstart, 6) == 0) {
  381. pix_fmt = AV_PIX_FMT_GRAY16;
  382. } else if (strncmp("mono", tokstart, 4) == 0) {
  383. pix_fmt = AV_PIX_FMT_GRAY8;
  384. } else {
  385. av_log(s, AV_LOG_ERROR, "YUV4MPEG stream contains an unknown "
  386. "pixel format.\n");
  387. return -1;
  388. }
  389. while (tokstart < header_end && *tokstart != 0x20)
  390. tokstart++;
  391. break;
  392. case 'I': // Interlace type
  393. switch (*tokstart++){
  394. case '?':
  395. break;
  396. case 'p':
  397. s1->interlaced_frame = 0;
  398. break;
  399. case 't':
  400. s1->interlaced_frame = 1;
  401. s1->top_field_first = 1;
  402. break;
  403. case 'b':
  404. s1->interlaced_frame = 1;
  405. s1->top_field_first = 0;
  406. break;
  407. case 'm':
  408. av_log(s, AV_LOG_ERROR, "YUV4MPEG stream contains mixed "
  409. "interlaced and non-interlaced frames.\n");
  410. return -1;
  411. default:
  412. av_log(s, AV_LOG_ERROR, "YUV4MPEG has invalid header.\n");
  413. return -1;
  414. }
  415. break;
  416. case 'F': // Frame rate
  417. sscanf(tokstart, "%d:%d", &raten, &rated); // 0:0 if unknown
  418. while (tokstart < header_end && *tokstart != 0x20)
  419. tokstart++;
  420. break;
  421. case 'A': // Pixel aspect
  422. sscanf(tokstart, "%d:%d", &aspectn, &aspectd); // 0:0 if unknown
  423. while (tokstart < header_end && *tokstart != 0x20)
  424. tokstart++;
  425. break;
  426. case 'X': // Vendor extensions
  427. if (strncmp("YSCSS=", tokstart, 6) == 0) {
  428. // Older nonstandard pixel format representation
  429. tokstart += 6;
  430. if (strncmp("420JPEG", tokstart, 7) == 0)
  431. alt_pix_fmt = AV_PIX_FMT_YUV420P;
  432. else if (strncmp("420MPEG2", tokstart, 8) == 0)
  433. alt_pix_fmt = AV_PIX_FMT_YUV420P;
  434. else if (strncmp("420PALDV", tokstart, 8) == 0)
  435. alt_pix_fmt = AV_PIX_FMT_YUV420P;
  436. else if (strncmp("420P9", tokstart, 5) == 0)
  437. alt_pix_fmt = AV_PIX_FMT_YUV420P9;
  438. else if (strncmp("422P9", tokstart, 5) == 0)
  439. alt_pix_fmt = AV_PIX_FMT_YUV422P9;
  440. else if (strncmp("444P9", tokstart, 5) == 0)
  441. alt_pix_fmt = AV_PIX_FMT_YUV444P9;
  442. else if (strncmp("420P10", tokstart, 6) == 0)
  443. alt_pix_fmt = AV_PIX_FMT_YUV420P10;
  444. else if (strncmp("422P10", tokstart, 6) == 0)
  445. alt_pix_fmt = AV_PIX_FMT_YUV422P10;
  446. else if (strncmp("444P10", tokstart, 6) == 0)
  447. alt_pix_fmt = AV_PIX_FMT_YUV444P10;
  448. else if (strncmp("420P12", tokstart, 6) == 0)
  449. alt_pix_fmt = AV_PIX_FMT_YUV420P12;
  450. else if (strncmp("422P12", tokstart, 6) == 0)
  451. alt_pix_fmt = AV_PIX_FMT_YUV422P12;
  452. else if (strncmp("444P12", tokstart, 6) == 0)
  453. alt_pix_fmt = AV_PIX_FMT_YUV444P12;
  454. else if (strncmp("420P14", tokstart, 6) == 0)
  455. alt_pix_fmt = AV_PIX_FMT_YUV420P14;
  456. else if (strncmp("422P14", tokstart, 6) == 0)
  457. alt_pix_fmt = AV_PIX_FMT_YUV422P14;
  458. else if (strncmp("444P14", tokstart, 6) == 0)
  459. alt_pix_fmt = AV_PIX_FMT_YUV444P14;
  460. else if (strncmp("420P16", tokstart, 6) == 0)
  461. alt_pix_fmt = AV_PIX_FMT_YUV420P16;
  462. else if (strncmp("422P16", tokstart, 6) == 0)
  463. alt_pix_fmt = AV_PIX_FMT_YUV422P16;
  464. else if (strncmp("444P16", tokstart, 6) == 0)
  465. alt_pix_fmt = AV_PIX_FMT_YUV444P16;
  466. else if (strncmp("411", tokstart, 3) == 0)
  467. alt_pix_fmt = AV_PIX_FMT_YUV411P;
  468. else if (strncmp("422", tokstart, 3) == 0)
  469. alt_pix_fmt = AV_PIX_FMT_YUV422P;
  470. else if (strncmp("444", tokstart, 3) == 0)
  471. alt_pix_fmt = AV_PIX_FMT_YUV444P;
  472. }
  473. while (tokstart < header_end && *tokstart != 0x20)
  474. tokstart++;
  475. break;
  476. }
  477. }
  478. if (width == -1 || height == -1) {
  479. av_log(s, AV_LOG_ERROR, "YUV4MPEG has invalid header.\n");
  480. return -1;
  481. }
  482. if (pix_fmt == AV_PIX_FMT_NONE) {
  483. if (alt_pix_fmt == AV_PIX_FMT_NONE)
  484. pix_fmt = AV_PIX_FMT_YUV420P;
  485. else
  486. pix_fmt = alt_pix_fmt;
  487. }
  488. if (raten <= 0 || rated <= 0) {
  489. // Frame rate unknown
  490. raten = 25;
  491. rated = 1;
  492. }
  493. if (aspectn == 0 && aspectd == 0) {
  494. // Pixel aspect unknown
  495. aspectd = 1;
  496. }
  497. st = avformat_new_stream(s, NULL);
  498. if (!st)
  499. return AVERROR(ENOMEM);
  500. st->codec->width = width;
  501. st->codec->height = height;
  502. av_reduce(&raten, &rated, raten, rated, (1UL << 31) - 1);
  503. avpriv_set_pts_info(st, 64, rated, raten);
  504. st->codec->pix_fmt = pix_fmt;
  505. st->codec->codec_type = AVMEDIA_TYPE_VIDEO;
  506. st->codec->codec_id = AV_CODEC_ID_RAWVIDEO;
  507. st->sample_aspect_ratio = (AVRational){ aspectn, aspectd };
  508. st->codec->chroma_sample_location = chroma_sample_location;
  509. return 0;
  510. }
  511. static int yuv4_read_packet(AVFormatContext *s, AVPacket *pkt)
  512. {
  513. int i;
  514. char header[MAX_FRAME_HEADER+1];
  515. int packet_size, width, height, ret;
  516. AVStream *st = s->streams[0];
  517. struct frame_attributes *s1 = s->priv_data;
  518. for (i = 0; i < MAX_FRAME_HEADER; i++) {
  519. header[i] = avio_r8(s->pb);
  520. if (header[i] == '\n') {
  521. header[i + 1] = 0;
  522. break;
  523. }
  524. }
  525. if (s->pb->error)
  526. return s->pb->error;
  527. else if (s->pb->eof_reached)
  528. return AVERROR_EOF;
  529. else if (i == MAX_FRAME_HEADER)
  530. return AVERROR_INVALIDDATA;
  531. if (strncmp(header, Y4M_FRAME_MAGIC, strlen(Y4M_FRAME_MAGIC)))
  532. return AVERROR_INVALIDDATA;
  533. width = st->codec->width;
  534. height = st->codec->height;
  535. packet_size = avpicture_get_size(st->codec->pix_fmt, width, height);
  536. if (packet_size < 0)
  537. return packet_size;
  538. ret = av_get_packet(s->pb, pkt, packet_size);
  539. if (ret < 0)
  540. return ret;
  541. else if (ret != packet_size)
  542. return s->pb->eof_reached ? AVERROR_EOF : AVERROR(EIO);
  543. if (st->codec->coded_frame) {
  544. st->codec->coded_frame->interlaced_frame = s1->interlaced_frame;
  545. st->codec->coded_frame->top_field_first = s1->top_field_first;
  546. }
  547. pkt->stream_index = 0;
  548. return 0;
  549. }
  550. static int yuv4_probe(AVProbeData *pd)
  551. {
  552. /* check file header */
  553. if (strncmp(pd->buf, Y4M_MAGIC, sizeof(Y4M_MAGIC) - 1) == 0)
  554. return AVPROBE_SCORE_MAX;
  555. else
  556. return 0;
  557. }
  558. #if CONFIG_YUV4MPEGPIPE_DEMUXER
  559. AVInputFormat ff_yuv4mpegpipe_demuxer = {
  560. .name = "yuv4mpegpipe",
  561. .long_name = NULL_IF_CONFIG_SMALL("YUV4MPEG pipe"),
  562. .priv_data_size = sizeof(struct frame_attributes),
  563. .read_probe = yuv4_probe,
  564. .read_header = yuv4_read_header,
  565. .read_packet = yuv4_read_packet,
  566. .extensions = "y4m",
  567. };
  568. #endif