yuv4mpeg.c 15 KB

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