yuv4mpeg.c 14 KB

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